extract specific file from list of folders
1 次查看(过去 30 天)
显示 更早的评论
I have 2 folders called EXP1, and EXP2, and within each of those folders is 4 folders, labelled 1-4. I have a code which adds each path to matlab, which may help visualize this:
clear
%add complete paths to every template file you will be using
addpath('C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\4',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\4');
Each of the text files is named EXP(n)_SQ(m)_Template, with the EXP folder number replacing n, and the subfolder replacing m.
I want a user to be able to input a specific number for an experiment, and squad, and have that file used for the remainder of the script.
Here is what I have:
clear
%add complete paths to every template file you will be using
addpath('C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT1\4',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\1',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\2',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\3',...
'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader\EXPT2\4');
user_input_exp = input('Enter Experiment Number: ', 's');
user_input_squad = input('Enter Squad Number: ', 's');
Mac_Templ = importdata('EXP(user_input_exp)_SQ(user_input_squad)_Template.txt');
I get this error:
Error using importdata (line 139)
Unable to open file.
Error in test_editing (line 17)
Mac_Templ = importdata('EXP(user_input_exp)_SQ(user_input_squad)_Template.txt');
If I take away the '' surrounding the file name, I get this error by the underscores:
Error: File: test_editing.m Line: 17 Column: 43
Invalid text character. Check for unsupported symbol, invisible character, or pasting of
non-ASCII characters.
Any help is appreciated.
0 个评论
采纳的回答
Guillaume
2019-12-5
First. you shouldn't be using addpath just to be able to load some data. addpath is intended to make code files visible, not data files. All the import functions can load data from any folder regardless of whether or not they're on the path. It's much better to pass the full path of the file to the the import function. With addpath you may end up importing a file with the same name but in a different folder than what you intended.
Secondly, why do assume that matlab is going to somehow guess that in the char vector 'EXP(user_input_exp)_SQ(user_input_squad)_Template.txt' it's supposed to replace the user_input_exp part by the variable of the same name, but not replace the Temp part by the value of a variable called Temp or even the a of Template by the value of a variable called a? Matlab doesn't yet have a mind reading toolbox, if you want to insert something from a variable into some text you have to tell it explicitly with functions such as sprintf, compose or by string concatenation and things like num2str.
So, here is how to code it:
root = 'C:\Users\Administrator\Dropbox (*******)\******* Team Folder\Matlab\RFID chip reader';
user_input_exp = input('Enter Experiment Number: ', 's');
user_input_squad = input('Enter Squad Number: ', 's');
filepath = fullfile(root, sprintf('EXPT%s', user_input_exp), user_input_squad, sprintf('EXP_%s_SQ%s_Template.txt', user_input_exp, user_input_squad));
assert(exist(filepath, 'file'), 'Selected Experiment number and squad number combination is not valid');
Mac_Templ = importdata(filepath);
Thirdly, importdata is the big gun of importing. If you actually know the type of file you're importing it would be better to use a more specialised import function (readtable or readmatrix maybe?). Your follow up code will assume a particular output from importdata but importdata will happily import any kind of file which may result in a completely different output than you expect. It's better to error at the import because the file is not in the expected format rather than for obscure reason later on because importdata imported the data in a different format.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!