CSVWRITE from listbox

1 vue (au cours des 30 derniers jours)
William
William le 17 Juin 2011
I am attempting to write to a CSV file from a listbox in which I have several varable displayed in. I need to write a csv file that is entitled with the name of the variable. in other words I have for example "variable_one" in the list box and I need to create a csv file that is entitled "variable_one"
so far I have used
a = get(handles.listbox2,'string')
csvwrite(a(1), "the matrix with te same filename") I am just starting out in matlab programming after having spent a few years in college making plots with it and writing basic script files.
Thank you

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 17 Juin 2011
Use eval() or evalin() to get the data from a string representing the variable name. You probably should also include '.csv' in your csv file name. In your example, a is probably a cell array of string, so you should do:
csvwrite([a{1},'.csv'],eval(a{1}))
or
csvwrite([a{1},'.csv'],evalin('base',a{1}))
Considering your previous question, I assume you want to write certain variables in a .mat file to a .csv file, using a GUI for the user to pick and choose the variables. In your call-back function, you can use Vars=load('MyData.mat') to load the data. All the variables will be put in the fields of the structure data Vars. Use VariableNames=fieldnames(Var) to get the list of variable names and use it for your list box. Once you get the chosen variable name in a{1} as shown in your example, Vars.(a{1}) is the data you want to write to your .csv file. Try this at Command line to understand and get familiar with the syntax and then implement it in your code. That way, you can avoid using the eval() function.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 17 Juin 2011
It is better if you can avoid doing this. Please read this FAQ
  1 commentaire
William
William le 17 Juin 2011
I'm not Sure I understand exactly what you are trying to explain to me. THank you for your help though I really appreciate it

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by