Inserting values from matrix into a script file
古いコメントを表示
I have a script in which some of the values should change from the matrix which i have and create a txt file.
Supoose my matrix is like [ 0 3846 6; 4154 51 64 ; 8945 56 4]
I should get three txt files like,first one
$$$$$$$$$$$$$$
My num= '0'
format = '3846'
heading='6'
2nd txt file
$$$$$$$$$$$$$$
My num= '4154'
format = '51'
heading='64'
3rd txt file
$$$$$$$$$$$$$$
My num= '8945'
format = '56'
heading='4'
This is the script which i should get,Could you please help me
Please note strings are mandatory on the numbers
2 件のコメント
madhan ravi
2019 年 1 月 29 日
Why did you delete the previous same question and posted a new one?
Jan
2019 年 1 月 29 日
What exactly is your question? What have you tried so far? "This is the script" is not clear. "strings are mandatory on the numbers" is not clear also.
回答 (1 件)
X = [ 0 3846 6; 4154 51 64 ; 8945 56 4];
Folder = 'C:\Temp'; % Adjust to yozur needs
fileNames = {'max', 'min', 'avg'};
for k = 1:size(X, 1)
FileName = fullfile(Folder, sprintf('%s.txt', filenames{k}));
[fid, msg] = fopen(FileName, 'w');
if fid == -1
error('Cannot open file: %s', msg);
end
fprintf(fid, ['$$$$$$$$$$$$$$\n', ... % [EDITED 2] comma added
'My num= ''%g''\n', ...
'format = ''%g''\n', ...
'heading=''%g''\n'], X(k, :)); % [EDITED]
fclose(fid);
end
5 件のコメント
Sandeep Nair
2019 年 1 月 29 日
編集済み: Sandeep Nair
2019 年 1 月 29 日
Jan
2019 年 1 月 29 日
"i require strings in the number" is not a meaningful statement. Strings are strings and numbers are numbers. Do you mean "quotes before and after the numbers"?
I forgot some values in my code. I've updated it now.
Sandeep Nair
2019 年 1 月 29 日
Sandeep Nair
2019 年 1 月 29 日
Jan
2019 年 1 月 29 日
Yes, this time I forgot a comma. But I hope, that you are able to fix some typos also. It is not so hard.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!