access specific element from importfile
2 views (last 30 days)
Show older comments
I have a data file “8545.txt” and I import the data using matlab and generate a matlab function (importfile.m) for that. So I have now the matlab generated file called ”importfile” and in the main program I write,
filename='8545.txt';
[t2,mass,time,chan,op,Com] = importfile(filename);
Now, if I want to access a specific value of “t2” for using in another place, how do I define an array (or something which works) which will enable me to access any specific value of “t2”. So, if t2 runs from 1 to 32, and I want to set some other variable in my code equal to a specific value of t2, i.e. f[t2] == m ….. something like that, how to do it?
For example, t2: 1 to 32, mass: 1 to 6, time: 0 to 64, chan: 1 to 4, op: 1 to 4, Com=complex number. So the data file looks like this:
1,1,1,1,1,3.273340e-08-1.351995e-09 i
1,1,1,1,2,6.339880e-09-1.015066e-09 i
1,1,1,1,3,-1.824081e-09-2.522303e-09 i
and so on. The last line is
32,6,64,4,4,-6.479969e-10-4.213433e-10 i
. So I would like to set some variable m equal to some specific value of t2
Thanks for your help.
2 Comments
per isakson
on 30 Sep 2014
Edited: per isakson
on 30 Sep 2014
The space between the number and "i" may cause a problem.
Answers (1)
per isakson
on 30 Sep 2014
Edited: per isakson
on 30 Sep 2014
Example
>> fid = fopen( 'complex.txt' );
>>
cac = textscan( fid, '%f%f%f%f%f%f', 'Delimiter', ',', 'Whitespace', '' )
cac =
Columns 1 through 5
[3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double]
Column 6
[3x1 double]
>> cac{6}
ans =
1.0e-07 *
0.3273 - 0.0135i
0.0634 - 0.0102i
-0.0182 - 0.0252i
>> fclose all
where complex.txt consists of
1,1,1,1,1,3.273340e-08-1.351995e-09i
1,1,1,1,2,6.339880e-09-1.015066e-09i
1,1,1,1,3,-1.824081e-09-2.522303e-09i
However, I failed to read the file with a space before the "i". To do that the approach needs to be something like
- read the file as text: str = fileread(complex.txt);
- str = strrep( str, ' ', '' );
- cac = textscan( str, '%f%f%f%f%f%f', 'Delimiter', ',', 'Whitespace', '' )
Thus, my question is whether you can avoid the space when writing the file.
If you remove the space you can use dlmread
M = dlmread( 'complex.txt' );
>> M(:,6)
ans =
1.0e-07 *
0.3273 - 0.0135i
0.0634 - 0.0102i
-0.0182 - 0.0252i
The second part of your question, I don't understand.
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!