MATLAB Answers

0

Changing the name of a variable throughout the code but only want it to change in a section.

Asked by Nichole Peterson on 30 Jul 2019
Latest activity Edited by Stephen Cobeldick on 31 Jul 2019
Below is an example of some code where I need to change the variable names. In the second section i need all the "TEM3" to be "TEM4". If you replce the name of one of the variables it comes up with a message saying "Press shift + enter to change the other '#' instences '....' is used"
Doing that changes all the variables in the code but id like it to only change the names in that section. Is there a wayto do that?
%% TEM3 - 0.25s Exposer
Grid = 0;
% Calling Images
TEM3_532_25 = imread('TEM3_NR_Y_532nm_Exp0.25s_100xMag_BG-C.tif');
TEM3_405_25 = imread('TEM3_NR_Y_405nm_Exp0.25s_100xMag_BG-C.tif');
% Eliminating Grid
TEM3_532_25_Copy = TEM3_532_25;
TEM3_532_25_Copy(TEM3_532_25_Copy<Grid)=0;
figure (1), imshow(TEM3_532_25_Copy)
TEM3_405_25_Copy = TEM3_405_25;
TEM3_405_25_Copy(TEM3_405_25_Copy<Grid)=0;
figure (2), imshow(TEM3_405_25_Copy)
%Normalization
MaxTEM3_532_25_Copy = max(TEM3_532_25_Copy);
MaxTEM3_405_25_Copy = max(TEM3_405_25_Copy);
NormTEM3_532_25_Copy = TEM3_532_25_Copy./MaxTEM3_532_25_Copy;
NormTEM3_405_25_Copy = TEM3_405_25_Copy./MaxTEM3_405_25_Copy;
% Image Overlay
TEM3Overlay1 = imfuse(NormTEM3_532_25_Copy,NormTEM3_405_25_Copy,'falsecolor','Scaling','independent','ColorChannels',[2 2 1]);
figure (3), imshow(TEM3Overlay1)
TEM3Overlay2 = imfuse(TEM3_532_25_Copy,TEM3_405_25_Copy,'falsecolor','Scaling','independent','ColorChannels',[2 2 1]);
figure (4), imshow(TEM3Overlay2)
%% TEM4 - 0.25s Exposer %%
Grid = 0;
% Calling Images
TEM3_532_25 = imread('TEM3_NR_Y_532nm_Exp0.25s_100xMag_BG-C.tif');
TEM3_405_25 = imread('TEM3_NR_Y_405nm_Exp0.25s_100xMag_BG-C.tif');
% Eliminating Grid
TEM3_532_25_Copy = TEM3_532_25;
TEM3_532_25_Copy(TEM3_532_25_Copy<Grid)=0;
figure (1), imshow(TEM3_532_25_Copy)
TEM3_405_25_Copy = TEM3_405_25;
TEM3_405_25_Copy(TEM3_405_25_Copy<Grid)=0;
figure (2), imshow(TEM3_405_25_Copy)
%Normalization
MaxTEM3_532_25_Copy = max(TEM3_532_25_Copy);
MaxTEM3_405_25_Copy = max(TEM3_405_25_Copy);
NormTEM3_532_25_Copy = TEM3_532_25_Copy./MaxTEM3_532_25_Copy;
NormTEM3_405_25_Copy = TEM3_405_25_Copy./MaxTEM3_405_25_Copy;
% Image Overlay
TEM3Overlay1 = imfuse(NormTEM3_532_25_Copy,NormTEM3_405_25_Copy,'falsecolor','Scaling','independent','ColorChannels',[2 2 1]);
figure (3), imshow(TEM3Overlay1)
TEM3Overlay2 = imfuse(TEM3_532_25_Copy,TEM3_405_25_Copy,'falsecolor','Scaling','independent','ColorChannels',[2 2 1]);
figure (4), imshow(TEM3Overlay2)

  1 Comment

Note that putting meta-data (e.g. indices, test parameters, sample numbers, etc.) into variable names is not a good practice. Accessing that meta-data forces users into writing code that is slow, complex, obfuscated, and hard to debug. It makes code much harder to work with:
Note that meta-data is data, and as such they should be stored in variables, not in variable names, as Jan already showed.

Sign in to comment.

Tags

Products


Release

R2018b

1 Answer

Answer by Jan
on 30 Jul 2019
Edited by Jan
on 30 Jul 2019

Copy the text block, which you want to modify, to a new editor window. Perform the wanted modifications. Copy the block back to the original file.
Alternatively replace all occurrences, copy the block in which the modifications are wanted, press Ctrl-Z to undo the changes, replace the block, which should keep the modifications.
This needs just some clicks. I do not see a reason to implement this as feature of the editor.
By the way, the names, e.g. TEM3_405_25_Copy and TEM3_405_25 look, like you store information in the names of variables instead of the values. This is typical for a meta-programming style. It would be much smarter to use e.g. a struct:
Tem.Index = 3
Tem.Number = 405;
Tem.Socket = 25;
Tem.Value = rand;
Then Tem can be stored in a struct array. As a magic side effect, there is no need to change names of variables in the editor anymore, because the information is produced dynamically during the code runs. This is more flexible and easier to maintain.

  0 Comments

Sign in to comment.