Acquire data from text files in MATLAB

后端 未结 2 431
傲寒
傲寒 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条回答
  •  梦毁少年i
    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
    

提交回复
热议问题