필터 지우기
필터 지우기

assign input to variable name

조회 수: 2 (최근 30일)
Trader
Trader 2012년 4월 19일
I'm putting together a script that builds a .mat file and I was wondering, how can I create an array based off the name of a string? Example:
file = 'data_abc';
full_path =['folder/',file,'.csv'];
import = importdata(full_path)
data_abc = import.data;
How do you take the string and convert it to a variable name?
Thanks!

답변 (1개)

Matt Tearle
Matt Tearle 2012년 4월 19일
Generally this isn't a great idea, but if you want to, you can use eval. First make a string of the command you want to issue:
str = [file,' = import.data;'];
Then evaluate it as a command:
eval(str)
(Obviously you can combine these lines into one command, but I'm showing the steps for clarity.)
An alternative approach is to use a structure variable, using the filename as the fieldname:
x.(file) = import.data; % equivalent to x.data_abc = ...
  댓글 수: 2
Daniel Shub
Daniel Shub 2012년 4월 19일
"it isn't a great idea" can be interpeted as "IT IS A REALLY BAD IDEA"
Walter Roberson
Walter Roberson 2012년 4월 19일
Structure field and use save() with the -struct flag.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by