フィルターのクリア

Repeating a process with changing file names, possibly with a loop.

16 ビュー (過去 30 日間)
Daniel
Daniel 2020 年 6 月 30 日
編集済み: Stephen23 2020 年 6 月 30 日
Hello,
I want to display the values of a 3x3 matrix in pseudocolors. I already found a way to do that for one at a time, but I need to do this with approx. 300 matrices.
My process for one picture is the following:
% a001 is a name of a variable. These would be counting up with the matrices.
% A001 is the name of the matrix. These are counting up to whatever many matrices there are.
a001 = pcolor(A001);
a001.FaceColor = 'interp';
caxis([0 1500]);
% the axes should be the same for comparing purposes.
saveas (a001, "a001.png")
I don't know how to repeat this process, so that the numbered elements count up and different pictures are saved without one overwriting another. Any help would be much appreciated.
I have that feeling, that i'm going to be referred to the FAQ where it says not to do this. But I honestly do not know how I would transfer the advice given there to my problem.
  3 件のコメント
Daniel
Daniel 2020 年 6 月 30 日
編集済み: Daniel 2020 年 6 月 30 日
First of all, thank you for your comment.
The matrices were loaded in from an excel file. The data in the cells were prepared in a way that copying and pasting into the command window resulted in creating the matrices. (I used the command "concatenate()" in excel to add parentheses and the semicolons)
This might be the dumbest way of going about that, but it works out. At least for these few matrices.
From Matlab's point of view it would be like creating the matrices one at a time, I guess.
Stephen23
Stephen23 2020 年 6 月 30 日
編集済み: Stephen23 2020 年 6 月 30 日
"The data in the cells were prepared in a way that copying and pasting into the command window resulted in creating the matrices... This might be the dumbest way of going about that, but it works out. At least for these few matrices."
It is not dumb at all: for three matrices, such an approach is perfectly usable. I would probably do something similar if I only had one or two matrices to deal with. And when first experimenting with data, e.g. doing some exploratory plots and such things, there is absolutely nothing wrong with doing everything by hand. That is how one can get a feeling for the data.
But...
once the task moves into a more systematic analysis, then the code also needs to follow. And what is a good approach for handling 3 matrices easily is not the same as a good approach for handling 30 or 300 or 3000 or 30000 or ....
Load your data in a loop, and then your task will be much easier:

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

採用された回答

Johannes Fischer
Johannes Fischer 2020 年 6 月 30 日
Assuming you dont need a001 etc.
% N is the number of matrices
for ii = 1:N
% load or create A
%...
a = pcolor(A);
a.FaceColor = 'interp';
caxis([0 1500]);
% create a filename depending on the for-loop index.
% The current index is inserted using the format string %04d, which means,
% that ii is placed in the string as a number with '4' digits, leading '0's
% and as a whole number ('d')
filename = sprintf('a%04d.png', ii)
saveas (a, filename)
end
At this point it may be useful to have a look at sprintf and how to format text.
And yes, this code is not 100% efficient, because you would recreate the plot in every iteration. Its better to create 'a' before the loop and then only change its underlying data.
% initialize a with some matrix A0 (3x3)
a = pcolor(A0)
a.FaceColor = 'interp';
caxis([0 1500]);
% N is the number of matrices
for ii = 1:N
% load or create A
%...
% change the data that is displayed in a
a.CData = A;
% create a filename depending on the for-loop index.
% The current index is inserted using the format string %04d, which means,
% that ii is placed in the string as a number with '4' digits, leading '0's
% and as a whole number ('d')
filename = sprintf('a%04d.png', ii)
saveas (a, filename)
end

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by