简体   繁体   中英

How to create matrix in MATLAB

I have a situation in which I retrieved data in for loop I am editing it to show whats happening, I have tried using your approach but it doesnt seems fit :(

I think that what you mean is:

text files with this format

Date; Time; Temp °C
05.08.2011; 11:00:47;23.75
05.08.2011; 11:01:21;23.69
05.08.2011; 11:01:56;25.69
05.08.2011; 11:02:16;23.63

code

q{1}=1.txt;
q{2}=5.txt;
for j=1:2 %2 files
    fname=q{j};
    fid=fopen(fname,'r');
    header=fgetl(fid);
    data=textscan(fid,'%s','delimiter',';');
    fclose(fid);
    data=data{:};
    day=data(1:3:end);
    hour=data(2:3:end);
    temp=str2double(data(3:3:end)); 
    n1=size(temp);
    m = zeros(n1, j);
    for i = 1:n1
        m(i,j) = temp;
    end  
end

Now I want to create a matrix of size 4x2(4 denotes 4 temperature reading), but when I try to loop this but it says Subscripted assignment dimension mismatch.

q{1}=1.txt;
q{2}=5.txt;
n2 = numel(q);
m = zeros(4,n2);
for j=1:n2 %2 files
    fname=q{j};
    fid=fopen(fname,'rt');
    header=fgetl(fid);
    data=textscan(fid,'%s%s%f','delimiter',';');
    fclose(fid);

    day = data{1};
    hour = data{2};
    m(:,j) = data{3}; 
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM