sprintf into a number

5 vues (au cours des 30 derniers jours)
Julien Maxime
Julien Maxime le 12 Oct 2022
Réponse apportée : Jan le 15 Oct 2022
Hello,
I have the simple following code and it does not work of course as sprintf makes a string and cannot be entered as a value in the matrix.
Mat=zeros(64,64,round(Zlen));
for i = 1:64
for j = 1:64
for k = 1:Zlen
Mat(i,j,k)=sprintf('Ch0%d_0%d(1,%d)', i, j, k)
end
end
end
I know how Matlab work and that I cannot just hope to supress the ' by magic but I have these Data Matrix that are name as the following Ch01_01 / Ch01_02 / ... / Ch01_64 / Ch02_01 / Ch02_02 / Ch02_64 / Ch03_01 / etc..
However I still cannot find a turnaround that issue. Could you help me ?
Thank you
  5 commentaires
Julien Maxime
Julien Maxime le 13 Oct 2022
What is stopping me is that when i open the .mat matrix, i have more than 4000 files opening in the workspace and to open them all with load command I would need a loop and call them one by one, coming back to my initial issue.
Stephen23
Stephen23 le 15 Oct 2022
"What is stopping me is that when i open the .mat matrix, i have more than 4000 files opening in the workspace and to open them all with load command I would need a loop and call them one by one, coming back to my initial issue."
So far everything you describe sounds like it could be avoided by LOADing into an output variable, as already shown.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 15 Oct 2022
Stephen an Walter hit the point.
% Store imported data in an array instead of polluting the workspace:
FileData = load('YourInputFile.mat');
% No loop over k required:
Mat = zeros(64, 64, round(Zlen));
for i = 1:64
for j = 1:64
Mat(i, j, :) = FileData.(sprintf('Ch0%d_0%d', i, j));
end
end

Plus de réponses (1)

Walter Roberson
Walter Roberson le 12 Oct 2022
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by