Workspace variable not available despite visibility in another variable

3 views (last 30 days)
Hi Together,
I have the problem that some signals disappear in workspace, when some signals of measurement files are renamded to variables, which I use in MATLAB code.
Here is the code:
if(exist('Testsignal_1','var'))
Current=Testsignal_1;
end
keepvars(end+1) = {'Current'};
After Run the variable Current and Testsignal_1 disappear. But in variable keepvars the signal Current is still avaible.
Furthermore I do this for other signals and for them, the variables are avaible in the workspace.
Signals and variables only occur once in the script.
I got also no failure in Command Window.
Do you have an idea, why have only for two signals this problem?

Accepted Answer

VBBV
VBBV on 30 Jul 2024
Edited: VBBV on 30 Jul 2024
if you want to store the variable names Testsignal_1 and Current then you need to use an indexing variable as below. If this code snippet is present in a bigger code structure which is repeated several times, then the index helps to store the variable names and signals for each run/ iteration. In your code, the name of the signal and variables are not character vectors but defined by a value.
If the if-end condition is not satisfied, then the signal and variable names will NOT appear in workspace. The below code will store both signal and variable names and would appear in workspace if the condition is satisfied.
k = 1; % iterator index
if(exist('Testsignal_1','var'))
Current{k}='Testsignal_1'; % signal name as character vector stored in cell array
k = k+1; % increment
end
keepvars(end+1) = Current; % store or assign the cell array to keepvars data array present in bigger code structure
  1 Comment
FD
FD on 30 Jul 2024
Edited: FD on 30 Jul 2024
Thank you for your fast reply.
The code is working but created a cell array.
But good news I found the failure in my code, so now it works :-)

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!