Help finding location of variable names
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luis Eduardo Cofré Lizama
el 16 de Jun. de 2016
Respondida: Luis Eduardo Cofré Lizama
el 19 de Jun. de 2016
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 comentarios
Shameer Parmar
el 16 de Jun. de 2016
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
el 16 de Jun. de 2016
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.
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 16 de Jun. de 2016
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 comentarios
Luis Eduardo Cofré Lizama
el 16 de Jun. de 2016
Editada: Walter Roberson
el 16 de Jun. de 2016
Walter Roberson
el 16 de Jun. de 2016
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
el 16 de Jun. de 2016
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 comentarios
Shameer Parmar
el 17 de Jun. de 2016
Editada: Shameer Parmar
el 17 de Jun. de 2016
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...
Ver también
Categorías
Más información sobre Large Files and Big Data en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!