CSVWRITE from listbox

조회 수: 6 (최근 30일)
William
William 2011년 6월 17일
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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 6월 17일
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.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 6월 17일
It is better if you can avoid doing this. Please read this FAQ
  댓글 수: 1
William
William 2011년 6월 17일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by