Acquire data from text files in MATLAB

后端 未结 2 429
傲寒
傲寒 2020-12-21 20:46

I have a big problem to Acquire a block of data structured in a particular way. Here\'s how the data are to be acquired (is a txt):

V|0|0|0|t|0|1|1|4|11|T4|         


        
相关标签:
2条回答
  • 2020-12-21 21:19

    take a look at textscan

    do you have any control over the format of the textfile?

    EDIT

    here's a rather hackish way to achieve the result

    function readtest()
    
    fid = fopen('test.txt'); 
    
    %skip 3 lines, save 4th, skip 5th
    for i = 1:4
        names = fgetl(fid);
    end
    fgetl(fid);
    
    % separate out names
    names = textscan(names,'%s','delimiter','|');
    
    % read the data
    data = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]','delimiter','|');
    
    fclose(fid);
    
    
    
    
    for i = 1:size(data,2)-1
        values = ( data{i}(1:end));
        if(iscell(values))
            values = cell2mat(values);
        end
    
        name = names{1}{i+1};
    
        % very basic error checking
        if(~strcmp(name, ''))
    
            %save the value in the calling work space
            assignin('caller', name, values)
        end
    end
    
    0 讨论(0)
  • 2020-12-21 21:34

    Any reason for Matlab? If you're in academia, you might have access to LabVIEW, which could be easier to learn for something like this. You'll want to use the Read from Text File VI, then parse the string. Of course, you'll have to make use of the "|" characters to separate the data (use the Match Pattern VI). You might eventually want to restructure the way data are stored to the text file too - use text keys rather than |. Something like:

    codserv N area | codice 1 nome N01 tnom 20 etc...

    Sorry for not providing an answer with some Matlab source but I would consider LabVIEW if it's an option.

    0 讨论(0)
提交回复
热议问题