How to store output for each input.

2 ビュー (過去 30 日間)
gurbhej_singh
gurbhej_singh 2019 年 5 月 17 日
コメント済み: gurbhej_singh 2019 年 5 月 17 日
Greetings for the day.
I am an civil enginnering student and new with the world of programing. I tried to load multiple inputs in program and run with for loop and succesfully achived the result for each input.
clc
inputNames = {"mat1.mat", "mat2.mat", "mat3.mat"}
length(inputNames)
for i = [1:length(inputNames)]
load(inputNames{i})
i
fprintf("Lx = Length of shorter span = %d mm \n", Lx)
fprintf("Ly = Length of longer span = %d mm \n", Ly)
endfor
Output:
inputNames =
{
[1,1] = mat1.mat
[1,2] = mat2.mat
[1,3] = mat3.mat
}
ans = 3
i = 1
Lx = Length of shorter span = 6000 mm
Ly = Length of longer span = 7000 mm
i = 2
Lx = Length of shorter span = 7000 mm
Ly = Length of longer span = 8000 mm
i = 3
Lx = Length of shorter span = 8000 mm
Ly = Length of longer span = 9000 mm
Now I want to save the result of each input in different output file.
For example if I run my program with three input files as in above code, i want to store the output in different files i.e, three files.
I really need help to solve this problem.
  2 件のコメント
KSSV
KSSV 2019 年 5 月 17 日
Which result?
gurbhej_singh
gurbhej_singh 2019 年 5 月 17 日
編集済み: gurbhej_singh 2019 年 5 月 17 日
i = 1
Lx = Length of shorter span = 6000 mm
Ly = Length of longer span = 7000 mm
i = 2
Lx = Length of shorter span = 7000 mm
Ly = Length of longer span = 8000 mm
i = 3
Lx = Length of shorter span = 8000 mm
Ly = Length of longer span = 9000 mm
Above given is the result I got when i run the code with Three input files and i want this result to be stored in different output file.
For Example resut given by "mat1.mat" file should be stored in "Output1" and like wise "Input2, Input3,....." should be saved in "Output2, Output3,....."

サインインしてコメントする。

採用された回答

Raj
Raj 2019 年 5 月 17 日
clc
inputNames = {'mat1.mat', 'mat2.mat', 'mat3.mat'}
length(inputNames)
for i = [1:length(inputNames)]
load(inputNames{i})
i
fileID = fopen(sprintf('file%d.txt',i),'w')
fprintf(fileID,'Lx = Length of shorter span = %d mm \n', Lx)
fprintf(fileID,'Ly = Length of longer span = %d mm \n', Ly)
fclose(fileID);
end
mat1 data will be stored in file1, mat2 in file2 and mat3 in file3.
  1 件のコメント
gurbhej_singh
gurbhej_singh 2019 年 5 月 17 日
Thank you very much, it works for me.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2019 年 5 月 17 日
inputNames = {"mat1.mat", "mat2.mat", "mat3.mat"}
length(inputNames)
for i = 1:length(inputNames)
load(inputNames{i})
i
fid = fopen(['output',num2str(i),'.txt'],'w') ;
fprintf(fid,"Lx = Length of shorter span = %d mm \n", Lx) ;
fprintf(fid, "Ly = Length of longer span = %d mm \n", Ly) ;
fclose(fid) ;
end
  1 件のコメント
gurbhej_singh
gurbhej_singh 2019 年 5 月 17 日
Thank you very much, it works for me.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by