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

Why did you delete the previous same question and posted a new one?
Jan
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개)

Jan
Jan 2019년 1월 29일
편집: Jan 2019년 1월 29일
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
Sandeep Nair 2019년 1월 29일
편집: Sandeep Nair 2019년 1월 29일
Hi Jan,
This text file am doing automation which i will use in some other software hence i require strings in the number since this is the format that the other software takes in
Please note i tried someting like this taking the format of the script in cell arrays and then printing it,please refer to the code below
X = [ 0 3846 6; 4154 51 64 ; 8945 56 4];
fileNames={max,min,avg}
for i = 1:numel(fileNames)
fileid = fopen(sprintf('%s.txt',fileNames{i}),'w')
a={'$$$$$$$$$$$$$$ \n',
'My num= ''%d''\n',
'format = ''%d''\n',
'heading=''%d''\n'};
for k=1:numel(a)
fprintf (fileid,a{k},X(i,1),X(i,2),X(i,3))
end
fclose(fileid);
end
But it is printing all the values ,hence i require a solution with full format including the $$$$$ signs
Jan
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.
Also Jan,please check the code provided by you as it is not even working,not able to print
the numbers as required
Sorry,yes you are right i mean to say quotes but still your code gives me following error
Dimensions of matrices being concatenated are not consistent.
Jan
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에 대해 자세히 알아보기

질문:

2019년 1월 29일

편집:

Jan
2019년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by