필터 지우기
필터 지우기

construct array name in script

조회 수: 1 (최근 30일)
Alex
Alex 2014년 8월 15일
댓글: Alex 2014년 8월 18일
New to MatLab by a month or so. I've looked through blogs and can't find and answer to this. I need to: 1) open a *.txt file which is comma delimited Nx3 'array' (I'm using uigetfile() for this) 2) pass the values to a Matlab array structure (I'm using csvread() for this) 3) Set the array structure name to the same as the name of the input file minus .txt (near is my problem) 4) write the array with the internal array name set to the input file name (I'm using save() for this)
My problem is I don't know how to construct the array name in script to pass onto the array written using the save() command.
Below is an elaboration with script segments:
% read a file into a variable selecting all files with a string '_element.txt' DbTitle = 'Generic title text' HeElements = uigetfile('*_element.txt', DbTitle) % HeElement is now a character string 'random_element.txt % remove '.txt' from the string HeLength = length(HeElements)-4 HeArrayName = strcat(HeElements(1:HeLength)) % HeArrayName now contains the filename without the '.txt' extension. This is the name I want assigned to the array eventually being saved to file. % Read array into variable HeArray = csvread(HeElements) % save array to a file save(HeFilename, 'HeArray')
The problem is the file that is saved, HeFileName, containes the proper array elements but with a literal name of the array being 'HeArray' with no quotes. I want the filename and array name to be the same, the string associated with the filename HeFileName without the extention ('He_element').
Thanks,
Alex
  댓글 수: 1
Evan
Evan 2014년 8월 15일
편집: Evan 2014년 8월 15일
Formatted code:
% read a file into a variable selecting all files with a string '_element.txt'
DbTitle = 'Generic title text'
HeElements = uigetfile('*_element.txt', DbTitle)
% HeElement is now a character string 'random_element.txt
% remove '.txt' from the string
HeLength = length(HeElements)-4 HeArrayName = strcat(HeElements(1:HeLength))
% HeArrayName now contains the filename without the '.txt' extension.
% This is the name I want assigned to the array eventually being saved to
% file.
% Read array into variable
HeArray = csvread(HeElements)
% save array to a file
save(HeFilename, 'HeArray')

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

채택된 답변

Jos
Jos 2014년 8월 16일
Hi Alex,
I think the following code using eval will do what you want
% read a file into a variable selecting all files with a string '_element.txt'
DbTitle = 'Generic title text'
HeFilename = uigetfile('*_element.txt', DbTitle)
% remove '.txt' from filename to get array name
HeArrayName = HeFilename(1:end-4);
% Read array into variable
eval([HeArrayName '= csvread(HeFilename)'])
% Save array to '.mat' file
eval(['save ' HeArrayName '.mat ' HeArrayName])
  댓글 수: 1
Alex
Alex 2014년 8월 18일
Thanks Jos! It worked perfectly! Being new to much of the functionality in MatLab, I'll have to review it to understand exactly why it worked. Apparently, eval can be used for some good:)
Thanks,
Alex

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by