How to convert string into filename then use load comment to load the file

조회 수: 33 (최근 30일)
angus wu
angus wu 2011년 5월 12일
i have a question on how to make use of string variable specifying a filename on the load command to load the data into the workspace. the partial code of the function i wrote is:
function input_data str1 = input('first part of filename:, 's'); str2 = input('second part of filename:, 's');
target_file = strcat(str1, str2, '_test_data');
then... % how to use load command to load the file specified by the target_file string variable into the working space.
running the function input_data should do:
first part of filename: first second part of filename: second
now the content of target_file should be 'firstsecond_test_data'
The question is how to make use of target_file with the load command so the the file firstsecond_test_data.mat can be loaded into the working space.
thanks

답변 (3개)

angus wu
angus wu 2011년 5월 12일
i solved it.
simply
load (target_file)
thanks all

Andy
Andy 2011년 5월 12일
Have you tried:
load [target_file '.mat']
If so, what didn't work the way you wanted?
EDIT: It turns out the use of brackets in this way with load and save is deprecated, and the brackets here are not just concatenation as I thought. The correct use, as you discovered, is
load target_file
Alternatively, you can use the functional syntax:
S = load(target_file)
to avoid 'poofing' variables into the workspace.

angus wu
angus wu 2011년 5월 12일
no. it does not work as
??? Error using ==> load Unable to read file [target_file '.mat']: No such file or directory.
Error in ==> inputfile at 4 load [target_file '.mat']

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by