2

I have a file like this:

ID  LHW  dms  
1   105.28 1 
2   357.01 0 
3   150.23 3

My question is if it is possible to get one column value based on the headline? I can of course get LHW by its column position, 2, but I would like to get it by just reading LHW.

The reason for this is that I have two large data sets (about 50 000 rows x 80 columns) with the same variables, but in different positions. If I could get the right column value based on its headline, it would save a lot of programming time.

Thanks in advance!

Community
  • 1
  • 1
KGB91
  • 630
  • 2
  • 6
  • 24

1 Answers1

2

I don't know about existing of some built-in function for your purposes. But we can create it manually. For example:

function [result] = readByName( A, filename )
if ~ischar(A) % check input parameter (you can delete this sectionif you want)
    display('Error: First argument must be a char array!');
    massiv = -1;
    return
end

fileID = fopen(filename);                       % open file
title = textscan(fileID, '%s',3);               % read title
number = cellfun( @(x) strcmp(x,A), title{1});  % find wanted column  

if ~any(number)   % one more check
    display('Error: wrong name of the first argument');
    massiv = -1;
    return
end

data = textscan(fileID, '%f %f %f');     % read data
result = data{ number==1 };              % get wanted column
end

Example of its implementation:

readByName('ID', 'yourdata.txt')

Answer:

ans =

 1
 2
 3

Now you can load data just by name. Now you have to modify it a bit if you will have columns with same name.

Hope it helps!

Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102
  • Thanks a lot! I however just get "Empty matrix: 0-by-1" as result. Can it be that I have misinterpreted the formate data file? – KGB91 Feb 02 '17 at 11:16
  • I think the problem is your real file is a bit different: my algorithm works (I checked it at your data example). You just need to adapt it to your data: 1. if you use a different number of columns correct `data = textscan(...)` string. 2. try to use debugger and find where exactly it goes wrong. 3. If there still be an error, put your code here, I will try to help you – Mikhail_Sam Feb 02 '17 at 11:41
  • I solved it by replacing the last line in your function (reslut = data...) with [DataColumn] = find(number == 1) – KGB91 Feb 02 '17 at 11:58
  • Thanks again for the help! :) – KGB91 Feb 02 '17 at 11:59
  • @KGB91 Aha! No problem! If my solution helped you can you mark it as a right answer? :) – Mikhail_Sam Feb 02 '17 at 12:26
  • Another option for your last couple lines (which may speed things up) is: `fStr = repmat({'%*f '}, 1, numel(number)); fStr(index) = {'%f '}; result = textscan(fileID, [fStr{:}]);` – gnovice Sep 18 '17 at 17:17
  • @gnovice I got the idea, but can you please clarify some moments: Why you use `%*f`? And second one - is `number` must be instead of `index`? – Mikhail_Sam Sep 19 '17 at 06:51
  • 1
    Yeah, that was a typo. `index` should be `number`. And `%*f` will skip that field instead of returning it. – gnovice Sep 19 '17 at 07:39