Repeating a process with changing file names, possibly with a loop.
古いコメントを表示
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 件のコメント
Stephen23
2020 年 6 月 30 日
"I have that feeling, that i'm going to be referred to the FAQ where it says not to do this."
Yes
Because when someone designs their data such that they force themselves into writing slow, complex, obfuscated, buggy code that is hard to follw and difficult to debug, some other helpful people are likely to suggest better ways of designing their data and code:
Not just helpful other users, but also the MATLAB documentation reccomends against your approach "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
"But I honestly do not know how I would transfer the advice given there to my problem."
The lede to that tutorial states "The most common alternative is to use simple and efficient indexing."
Have you tried using indexing instead of numbering your variables? I very much doubt that you sat and wrote out the names of 300 variables, so most likely your situation fits into either of these common cases:
- the arrays were load-ed from file, or
- the arrays were created in a loop.
Once you explain the most important information missing from your question (how did those arrays get into the workspace?) then someone can show you a better approach that will make repeating your code easy and efficient.
"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:
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrices and Arrays についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!