try and catch prob

2 次查看(过去 30 天)
Luis Eduardo Cofré Lizama
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

回答(1 个)

Walter Roberson
Walter Roberson 2019-1-9
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 个评论
Luis Eduardo Cofré Lizama
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
Walter Roberson
Walter Roberson 2019-1-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by