Loop "Replace String" from different string arrays

Hi
I have different strings which replace as below:
First array:
Second array:
Result array:
My Matlab code looks now as following:
% Replace MaterialIndex with effective colors
[ispresent, idx] = ismember(I1, ColorsIndexed(:,2));
I1(ispresent) = ColorsIndexed(idx(ispresent), 1);
% --------------------------------------------------------
% Replace MaterialIndex with effective colors
[ispresent, idx] = ismember(I2, ColorsIndexed(:,2));
I2(ispresent) = ColorsIndexed(idx(ispresent), 1);
I did it this way as I was unable to loop this operation. How can I loop this for I(n) arrays?
Best regards

4 Comments

How is I1, I2 present in the workspace?
As shown in the 2nd and 3rd picture, both as 1x402 string array. Or do you mean where these come from?
"how can I now use this for an "n" amount of arrays? Is there no loop necessary? I may can have 'I1' - 'I30', or 'I1' - 'I100'"
Numbering variables like that is a sign that you are doing something wrong.
Putting meta-data (like pseudo-indices) into variable names is a sign that you are doing something wrong.
Accessing badly designed data which is stored in lots of separate variables forces you into writing slow, complex, inefficient, buggy, difficult to debug code:
In most cases the simple, efficient, and recommended alternative is to use indexing into one variable (which could be a container array, e.g. a cell array or a table).
Hi,
Thank you for your reply. I was now able to solve it this way, everything stored in one array:
% Get all color indexes and replace MaterialIndex with effective colors
for k = 1:NumberMaterialIndex;
I{k} = string(strsplit(MatString{k}));
[ispresent, idx] = ismember(I{k}, ColorsIndexed(:,2));
I{k}(ispresent) = ColorsIndexed(idx(ispresent), 1);
end
Thank you very much!

Sign in to comment.

Answers (1)

Try this:
ColorIndexed = [compose("%.4f %.4f %.4f", rand(145,3)), compose("%3d",(1:145).')]; % Create String Array
I1 = compose("%d",randi(145,1,402)); % Create String Aray
I2 = ColorIndexed(sscanf(char(I1),'%3d'),1); % Use Indexing To Assign Colours
.

4 Comments

Hi
Thanks for your reply. My arrays look now like this:
It is not really what I intend, the 2nd one should look like this:
And I have the feeling it does not solve my issue with loopin it with n string arrays.
Regards
I created my arrays because I did not have yours to work with. A picture may be worth a thouand words, however actual data is necessary to provide an appropriate response.
No loops are necessary. Just use the indexing approach I used, and the ‘I2’ result should be what you want.
Ah ok, now I got it. I used your last line and it did work, thanks.
But how can I now use this for an "n" amount of arrays? Is there no loop necessary? I may can have 'I1' - 'I30', or 'I1' - 'I100'. It depends on the initial data I want to process. Sry I'm not so adept with Matlab.
I generate the arrays this way:
for k = 1:NumberMaterialIndex;
eval(sprintf('I%d = string(strsplit(MatString{k}))', k)); %funktioniert einwandfrei
end
Regards
I wrote code for 1 array. For ‘n’ arrays, you would need to loop through the arrays and do the same as I did for each one.
Please do not use the eval function! I have absolutely no idea what you are doing in the loop you posted, however I am reasonably confident that there is a much better and more efficient way to do it.
For example, put all the different arrays in a cell array (the cat function is likely appropriate here), and then just index them in the loop. Store the output in a matching cell array, created with a slightly different name for each of the result arrays so as not to over-write the original cell array of string arrays.

Sign in to comment.

Products

Release

R2020a

Asked:

on 8 Oct 2020

Commented:

on 15 Oct 2020

Community Treasure Hunt

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

Start Hunting!