How to multiply each column in the matrix by different scientific notation values that should be imported from a txt file?
4 次查看(过去 30 天)
显示 更早的评论
I have data in a txt file that I imported into Matlab.
The file has val(:,:,1), val(:,:,2), and so on all the way to val(:,:.256) and each array contains 128 data points.
I imported these data so that it’s in a 128x256 (column x row) matrix using the following:
filename = 'SeqLoop.data.kSpaceOS.txt';
fid = fopen(filename,'r');
S = textscan(fid,'%s','delimiter','\n');
fclose(fid);
S = S{1};
S = str2double(S);
idx = isnan(S);
S = S(~idx);
S = reshape(S,128,[]);
The first line of all the arrays contains a scientific notation that must be multiplied by the following cells in that array.
For example, the first line of val(:,:,2) is 1.0e-07 * followed by 128 data points.
The coding above does not multiply the data points by the scientific notation value.
And the value of the scientific notation changes for each array; i.e., the value of scientific notation for val(:,:,1), val(:,:,2), and so on are different.
How can I implement this into my coding so that the value of the scientific notations is accounted for in my matrix? (Please see attached for the txt file).
6 个评论
Cris LaPierre
2021-11-18
No. If you are seeing it in a text file, you are not saving it to a *.mat file.
采纳的回答
David Goodmanson
2021-11-18
编辑:David Goodmanson
2021-11-19
HI Gabrielle,
MODIFIED
Looks like you have constructing the 128x256 matrix of values covered. The code below converts S{1} to a character matrix and parses the result to obtain the exponents.
In the text file, the first header does not have the correct line to supply the exponent, and neither do headers 127-130, so I modified the text file before importing it, using e-07 for the first case and e-03 for the others.
filename = 'SeqLoop.data.kSpaceOS_128ETL_new.txt';
fid = fopen(filename,'r');
S = textscan(fid,'%s','delimiter','\n');
fclose(fid);
S = S{1};
E = char(S); % note added line
S = str2double(S);
idx = isnan(S);
S = S(~idx);
S = reshape(S,128,[]);
ind = any(E =='*',2); % rows containing the exponent
E = E(ind,:);
E(E=='*') = ' ';
z = round(log10(str2num(E)))'; % find the exponent (includes its sign)
format compact
z
format
tenz = 10.^(z);
result = repmat(tenz,128,1).*S;
5 个评论
David Goodmanson
2021-11-19
编辑:David Goodmanson
2021-11-19
HI Gabrielle,
the single zero does make a difference, since the code was designed for two digits after the 'e', and one digit throws off the steps that use columns 7 and 9. I modified the answer so as to be more robust for entries such as 1.0e-0. Please let me know if it works.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!