Hi All, I am trying to read column headers in a file, however, sometimes the header varies by adding "_L" or "_R" to the name. I want to identify the row where the headers are using try/catch so if one set of variable names doesnt work then I use the modified one as below. Unfortunately, this is not working. I am expecting to get the second "emgvar" if the first one gives me a mismatch error. Help please.
emgvar = {'Spare_1';'Spare_2';'CH3_GME_';'CH4_GME_';'CH5_RF_L';'CH6_RF_R';'CH7_VL_L';'CH8_VL_R';'CH9_HB_L';'CH10_HB_';'CH11_TA_';'CH12_TA_';'CH13_GL_';'CH14_GL_';'CH15_SOL';'CH16_SOL'};
for n = 1:length(data)
if ~isempty(strfind(data{n},emgvar{1,1}))
E_rowNumber = n;
E_rowData = data{n};
splitRowData_E = regexp(E_rowData,' ','split');
try
for a = 1:length(emgvar (:,1))
E_loc (a,1) = find(ismember(splitRowData_E,emgvar {a,1}),1);
end
catch ME1
idSegLast = regexp (ME1.identifier, '(?<=:)\w+$','match');
if strcmp(idSegLast,'InvalidFid')
emgvar = {'Spare_1';'Spare_2';'CH3_GME_L';'CH4_GME_R';'CH5_RF_L';'CH6_RF_R';'CH7_VL_L';'CH8_VL_R';'CH9_HB_L';'CH10_HB_R';'CH11_TA_L';'CH12_TA_R';'CH13_GL_L';'CH14_GL_R';'CH15_SOL_L';'CH16_SOL_R'};
end
try
for a = 1:length(emgvar (:,1))
E_loc (a,1) = find(ismember(splitRowData_E,emgvar {a,1}),1);
end
catch ME2
fprintf('No data found');
end
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 9 Jan 2019

0 votes

Why are you checking MATLAB:FileIO:InvalidFid ? That section of code is not reading a file.
If there are no elements matching that variable then the ismember() would return a vector that is completely false, and the find(,1) of that would return empty. You would then have an error from trying to assign emptiness to something. That would give you a MATLAB:subsassigndimmismatch error. Try/catch is a pretty heavy-handed way to do your checking.
I am a left uncertain in your code. Is each column individually subject to occur in base form or base_L form or base_R form ? Other than Spare_1 and Spare_2 ? Are CH3 through CH16 all required to appear, or can some be missing?

4 Comments

Luis Eduardo Cofré Lizama's "Answer" moved here:
Thanks Walter. I think I just did not undertstand well the part of the code you are referring to as I was following a youtube tutorial. Anyways, basically I am trying to get the number of the row in which these names apper to then split it to get the column in which each are. However, I have to options for the variables names in "emgvar": a) the one expected (somne with _L or R at the end) and b) in which all have "L" and _R" at the end. Perhaps tehre is an easier way to say "find this and if doesnt exist then find this other one (which is similar)". PS: this occured 'cos a change in another soiftware creating this txts.
regards
eduardo
Is it the case that any one file will have the columns
Spare_1 Spare_2 CH3_GME_ CH4_GME_ CH5_RF_L CH6_RF_R CH7_VL_L CH8_VL_R CH9_HB_L CH10_HB_ CH11_TA_ CH12_TA_ CH13_GL_ CH14_GL_ CH15_SOL CH16_SOL
in that order, or else will have the columns
Spare_1 Spare_2 CH3_GME_L CH4_GME_R CH5_RF_L CH6_RF_R CH7_VL_L CH8_VL_R CH9_HB_L CH10_HB_R CH11_TA_L CH12_TA_R CH13_GL_L CH14_GL_R CH15_SOL_L CH16_SOL_R
in that order, and there is no other possibility?
yep, excactly same order, just "_L" and "_R" differ for some names (e.g. CH3_GME_ CH4_GME_ CH10_HB_ CH11_TA_ CH12_TA_ CH13_GL_ CH14_GL_ CH15_SOL CH16_SOL). It seems that characters length number got restricted to 7 in my new files and that's why I had the prob with my script.
regards
eduardo
You know you can strcmpn() ? Though now I am wondering why you are bothering to do this checking, when it sounds like you could simply look for lines that start with Spare_1 and then assume that the columns are the correct purpose and the correct order.

Sign in to comment.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products

Release

R2017a

Tags

Community Treasure Hunt

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

Start Hunting!