How to access Variables with Incremental Names in a Loop

Hello everyone.
I have double varriables with names X097_DE_time ;X098_DE_time ; X098_DE_time...
i want to extract date from them in a loop
like
for i=97:1:103
a= Xi_DE_time% as an example if i=98 i want to put X098_DE_time in a
%some calculation with a
end
is it possible to this in matlab?
if yes how can i do that ?

 Accepted Answer

Jan
Jan on 27 Apr 2021
Edited: Jan on 27 Apr 2021
"with names X097_DE_time ;X098_DE_time" - this is the main problem. The solution is easy: Do not hide an index in the names of variables, but create an array and use indices as indices.
With using indices in arrays, the solution is trivial. So do not search for a complicated method to access a bunch of variables dynamically, but fix the code, which produces the variables.

4 Comments

thanks a lot
but I did not create this database ,data points were labled in bearing test of Case Western University
so i want to put their values in an easier structure in order to work with them.
The problem is not, who has created the data set, but how it was imported. Creating a bunch of variables e.g. by calling load(File) without catching the outputs in an array, would be one of the pitfalls for beginners.
So please post the code you are using to import the data, because this is the location where the fix needs to be applied.
"but I did not create this database ..."
How the database was created is irrelevant: you are importing, so you have complete control over how it ends up in the MATLAB workspace. Most likely you could load into an output variable or similar:
S = load(..)
thanks my friends
i use fieldnames() and it works .
it was very helpful guys

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!