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 个评论
parslee
parslee 2021-11-17
It doesn't work. It still outputs it like the txt file above.
Could the reason as to why it's not working be due to the matrix being 128x1x256 complex double?

请先登录,再进行评论。

采纳的回答

David Goodmanson
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
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 CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by