How to multiply each column in the matrix by different scientific notation values that should be imported from a txt file?
1 回表示 (過去 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 で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!