extract specific file from list of folders

1 visualización (últimos 30 días)
avram alter
avram alter el 5 de Dic. de 2019
Editada: avram alter el 5 de Dic. de 2019
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.

Respuesta aceptada

Guillaume
Guillaume el 5 de Dic. de 2019
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.
  1 comentario
avram alter
avram alter el 5 de Dic. de 2019
Editada: avram alter el 5 de Dic. de 2019
all excellent points. I am relatively new to matlab, and coding in general, so a lot of the nuance is currently over my head.
Edit: I figured out the problem, but the assert function does not work correctly.
I have adapted your code in 2 places. one is exactly where I said it would go, finding the file. I put the other to rename the file, and save it in a specific location. when running your code, I got an error by the assert line that
The condition input must be a scalar logical
when I comment the assert line, I get
Error using importdata (line 139)
Unable to open file.
Error in test_editing (line 24)
Mac_Templ = importdata(Templ_filepath);
I have appended the full code, with the bits unrelated to this within a comment block:
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);
% script to change specific strings in retreived file to user inputted strings
user_input_stage = input('Enter correct Stage: ','s');
todaysMac = Mac_Templ;
todaysMac = strrep(todaysMac, '(m)', user_input_stage);
user_input_session = input('Enter correct Session: ','s');
todaysMac = strrep(todaysMac, '(n)', user_input_session);
user_input_list = input('Enter correct List: ','s');
todaysMac = strrep(todaysMac, '(x)', user_input_list);
todaysMac = string(todaysMac);
todaysMac = compose(todaysMac); % There was an issue with the files, where
% the output would be in a single line. this fixes it, by using the escape
% characters located at the end of each lin in the template (\n), compose
% basically reads those as a command, placing each box into a new line.
fid = fopen(fullfile('C:\Users\Administrator\Dropbox (********)\******** Team Folder\Matlab\RFID chip reader\Completed_Macros', sprintf('EXP_%s_SQ%s_completed.mac', user_input_exp, user_input_squad)), 'wt'); % Makes a new file in
%specified directory, with specified name and format at the end. saving a
%second file with the same name will overwrite older file.
fprintf(fid, '%s', todaysMac);
fclose(fid);
disp('Done');
It very well could be I made a mistake in my own script.
EDIT: I figured it out, it was just a misplaced underscore. thanks for the help, fantastic answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by