error in writematrix command (using loop)
Mostra commenti meno recenti
I have a syntax problem with a code. I want to use writematrix command in order to get output files from my code. Previously in my code I use for in order to create a loop. I would like for each loop to create a specific output. I use
for z=1:size(d2,1) %d2 is a matrix from input file)
....
writematrix(A,'output',num2str(z),'.xlsx') % A is an output matrix I want to create
end
but command window shows me an error.
Could you please help me?
8 Commenti
dpb
il 14 Feb 2021
You're undoubtedly intending
writematrix(A,"output"+z,'.xlsx')
using the newfangled string class with its automagic conversion and catenation via "+"
The oldfangled way would have been
writematrix(A,num2str(z,'output%02d.xlsx'))
Ivan Mich
il 15 Feb 2021
dpb
il 15 Feb 2021
First does, too, if don't forget to add the other "+" after pasting original...
>> z=1;
>> "output"+z+".xlsx"
ans =
"output1.xlsx"
>>
It doesn't have the nicety of the leading zero in the numeric field to ensure sorting in numeric order the other does, compose could take care of that.
Isaac Oguntoye
il 14 Set 2021
Jan
il 14 Set 2021
@Isaac Oguntoye: Your question is not meaningful. writematrix does not "support" num2str. The problem is, that the 2nd input argument of writematrix must be the file name. It does not matter, how you create it, but it must be provided as one argument. The OP used 3 arguments:
writematrix(A, 'output', num2str(z), '.xlsx')
% 1. 2. 3.
The solution is to join the 3 parts:
writematrix(A, ['output', num2str(z), '.xlsx'])
% or
writematrix(A, sprintf('output%d.xlsx', z))
% or
writematrix(A, 'output' + z + '.xlsx')
Isaac Oguntoye
il 14 Set 2021
@Jan Not meaningful? Sounds a little harsh. Ya, should have conveyed my thoughts better. Thanks for your suggested helpful solutions!
Jan
il 14 Set 2021
Sorry, @Isaac Oguntoye, I'm not a native speaker. With "not meaningful" I meant, that there is no logical meaning. One command cannot support another command. I do not have emotions when I answer questions in the forum and do not assess their background. I just wanted to tell you, that there is no magic relation between writematrix anf num2str.
Isaac Oguntoye
il 14 Set 2021
@Jan, Thank you so much for responding! Yes, I understand. I guess I wanted to say something like can num2str be used as an attribute within the writematrix function or as an option (like Name, Value, Delimiter) in the writematrix function? Even this isn't the perfect way to ask the question but you have answered well. I used the first part of your solution in your first response (that is :- writematrix(A, ['output', num2str(z), '.xlsx']))and it worked for me. Thanks so much!
Risposte (0)
Categorie
Scopri di più su Workspace Variables and MAT Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!