Help finding location of variable names

6 次查看(过去 30 天)
I need to read a specific row (20011) from a .txt file that contains my variables names, however, there row starts with tabs and has 2 tabs between names. Any idea how to 1) read the row and 2) identify the location of specific names. I tried all options from the import tool but none worked! The row looks like this:
MS1:LFHD MS1:RFHD MS1:LBHD MS1:RBHD
for example: look for LFHD should give me 3
Thanks!
  2 个评论
Shameer Parmar
Shameer Parmar 2016-6-16
For your second point to identify the location:
I have question.. In give example, can you first explain how it is 3 for LFHD, so that I can think and give you better solution..
Is it like..
for first TAB, it is 1
for first MS1, it is 2
So, for LFHD, it is 3... ????
Also tell, if you look for LBHD, then what should be the answer ?
Walter Roberson
Walter Roberson 2016-6-16
My guess was that the first TAB marks the end of empty column 1, the second TAB marks the end of empty column 2, and then the variable is in column 3.

请先登录,再进行评论。

采纳的回答

Luis Eduardo Cofré Lizama
Thanks a lot Shameer!. Its working beautifully! (just removed the +1 in location = find(ismember(splitRowData,'RBHD'))+1).
Cheers! PS: pretty sure many people extracting VICON data will be really appreciated too!

更多回答(2 个)

Walter Roberson
Walter Roberson 2016-6-16
textscan() with repeat count 1, HeaderLines 20010, Delimiter '\t', multiple delimiters as one, and use a format of repmat('%', 1, number_of_variables_expected) . Then in the cell array that results, ismember() to locate particular variables. To calculate the column number, multiply the cell index by 3.
  2 个评论
Luis Eduardo Cofré Lizama
编辑:Walter Roberson 2016-6-16
Thanks Walter!. here's the code, but it is not working. I am probably doing something wrong.
regards
textscan(fileID,'%f',1,'HeaderLines',20008,'Delimiter','\t','MultipleDelimsAsOne',1, repmat('%', 1,43))
Walter Roberson
Walter Roberson 2016-6-16
fmt = repmat('%s', 1, 43);
strings_cell = textscan(fileID, fmt, 1, 'HeaderLines', 20008, 'Delimiter', '\t', 'MultipleDelimsAsOne', 1);
strings = cellfun(@(C) C{1}, strings_cell, 'Uniform', 0);

请先登录,再进行评论。


Shameer Parmar
Shameer Parmar 2016-6-16
Hello Luis,
Consider you have LFHD.txt file having 50,000 lines of code.
Keep it in your current directory and run following code:
1. Following code is to read the txt file and store its content into array variable " data
% code to read the txt file and store contents into array variable " *data*"
clear all;
count = 1;
fid = fopen('LFHD.txt.txt');
tline = fgetl(fid);
while ischar(tline)
if (tline ~= -1)
data(count,:) = {tline};
else
data(count,:) = {''};
end
count = count + 1;
tline = fgetl(fid);
end
fclose(fid);
2. Now, use following code to serach the row number for your specific name (e.g. LFHD)
for i = 1:length(data)
if ~isempty(strfind(data{i},'LFHD'))
rowNumber = i;
break;
else
if (i==length(data))
msgbox('No data found');
end
end
end
3. after running the above codes, if you check for variable "*rowNumber*", it will give you the row number in which your specific name is present.
Note: This code gives very first row number in which the specific name is present.
for example: if row 5 and 8 both having the specific Name (i.e. LFHD) then this code will give you the answer 5 and it will break the loop.
4. for your second point for location: First tell me how you calculated the value 3 for LFHD, so that I can give you the code for it.
5. with following code, you can find the location for LFHD.
location = strfind(data{i},'LFHD');
but, it gives answer as 6 and not 3,
it is because it is considering 1 for TAB, 2 for M, 3 for S, 4 for 1, 5 for :(colon) and 6 for LFHD..
If value 6 is correct then keep this line of code for 'location' just before 'break' statement.
  2 个评论
Luis Eduardo Cofré Lizama
Thanks Shameer!. It is working!. The only prob is that the location, as you mentioned, considers "1 for TAB, 2 for M, 3 for S, 4 for 1, 5 for :(colon) and 6 for LFHD". This is a problem when trying to identify RBHD, in which case the answer is 36. This wouldn't be a problem if all variable started with 'MS1:" but unfortunately some don't. How can I disregard that bit to get, for instance, location 3 for LFHD and 12 for RBHD?. So far I can manage with the code you provided tanks a lot!, but am struggling with those with no prefix 'MS1:".
Regards
Eduardo
Shameer Parmar
Shameer Parmar 2016-6-17
编辑:Shameer Parmar 2016-6-17
Hello Luis,
That why I asked you to give me the details on how you calculated value 3 for LFHD..
But, as you mentioned value 12 for RBHD, along with value 3 for LFHD, I created code for calculating the 'location'.
%Code for finding location as per given information like for LFHD, location=3 and for RBHD, location=12 as follows:
rowData = strrep(data{i},'MS1:','');
splitRowData = regexp(rowData,' ','split');
location = find(ismember(splitRowData,'RBHD'))+1
and the complete code as follows:
% code to read the txt file and store contents into array variable "data"
clear all;
count = 1;
fid = fopen('LFHD.txt');
tline = fgetl(fid);
while ischar(tline)
if (tline ~= -1)
data(count,:) = {tline};
else
data(count,:) = {''};
end
count = count + 1;
tline = fgetl(fid);
end
fclose(fid);
for i = 1:length(data)
if ~isempty(strfind(data{i},'RBHD'))
rowNumber = i
% code for finding location as per given information
% like for LFHD, location=3 and for RBHD, location=12
rowData = strrep(data{i},'MS1:','');
splitRowData = regexp(rowData,' ','split');
location = find(ismember(splitRowData,'RBHD'))+1
break;
else
if (i==length(data))
msgbox('No data found');
end
end
end
Using above code, I am getting answer as:
for LFHD - location = 3
for RFHD - location = 6
for LBHD - location = 9
for RBHD - location = 12
I hope this the same you are looking for...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by