Borrar filtros
Borrar filtros

Dynamically calling variables (it's not what this sounds like!)

8 visualizaciones (últimos 30 días)
RP el 9 de Nov. de 2021
Comentada: Mathieu NOE el 10 de Nov. de 2021
Hi everyone,
I have five files that have almost the same name except for one term that is added in the end, like "filename_A", "filename_B" etc., and the columns in these files are almost the same up to that last term as well, e.g.
filename_A: column1_A column2_A column3_A ...
filename_B: column1_B column2_B column3_B ...
I would like to loop through the different files and the different columns, but I cannot figure out how to dynamically bring up the names in the loop with sprintf or similar functions. I know people strongly advise against dynamically creating variables. But is that also the case for calling them?
I suppose an alternative would be to have created my files/named the columns in a better way, but I don't know how. Any help is greatly appreciated because I run into this problem all the time with the way my data is structured!
  5 comentarios
RP el 10 de Nov. de 2021
Thank you so much, this works too! I could only accept one answer so I picked Dave B's because it works best with the rest of my script (which I did not show here), but I saved your solution for future use!
Mathieu NOE
Mathieu NOE el 10 de Nov. de 2021
yes sure - there are many ways to tackle each specific problem and it's completely normal that you chose the one that matches best your application
... myself I learn a lot from other's answers and that's the positive aspect of this forum. There's always something new to (re) discover and I do also keep interesting answers in my PC for future use
all the best

Iniciar sesión para comentar.

Respuesta aceptada

Dave B
Dave B el 9 de Nov. de 2021
I think, if I read your question correctly, you're saying:
  • You have files with a suffix
  • The suffix determines the names of variables in a table?
  • You want to do something where you can use the filename's suffix to refer to the table variable names
If that's the case, I suspect the tricky bit is the syntax for referring to a table variable using a name. (Note the parantheses below, the general syntax is table.(dynamicname), which also works for stucts)
t = table(zeros(5,1),ones(5,1),'VariableNames',["Var_A" "Var_B"]);
suffixes=["_A" "_B"];
for i = 1:numel(suffixes)
disp(t.("Var" + suffixes(i)))
But if I missed something, it would certainly be helpful if you included some example data files or a reduced example to show what you're trying to accomplish.
(And yes I think most folks would agree it's be better to not create the files this way, but it's hard to say how you should change that code because you didn't give much info, maybe that's a separate question?)
  5 comentarios
RP el 10 de Nov. de 2021
Thank you so much for your help, I did not know at all that you can use something like
mtval = t.(var(i) + "_MT")(ind);
and it solved the issue perfectly! It's very elegant and short as well, thanks! Also the tip about using the name as a bit of data in an extra column is helpful for the next time I create these tables
RP el 10 de Nov. de 2021
@Steven Lord Thanks a lot for your answer, I tried it and it works! While I think Dave B's solution is more convenient in my case, I can see a lot of use cases in my script for your solution and I saved it in a script for the future, so thank you very much!

Iniciar sesión para comentar.

Más respuestas (0)


Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.





Community Treasure Hunt

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

Start Hunting!

Translated by