Matlab - 如何在嵌套for循环中将值写入excel

时间:2015-06-22 16:05:08

标签: matlab

我的图像是1944(R)x 2592(C)。我想一次拿一个专栏;将每列视为图像并计算该列的每行中包含的值的像素数>该列的一半(最大)值。这些像素数将被写入与其相应列相对应的excel表。

图片:the image

这是我到目前为止所尝试的内容,我无法成功编写。

 clc;
    sig = rgb2gray(imread('1.bmp')); % read in the image.
    % imshow(sig);
    ArraySize = size(sig); %1944(R) x 2592 (C)
    [maxval, maxloc] = max(sig(:)); % Gives the max and the location
    maxval; % max value
    [maxloc_row, maxloc_col] = ind2sub(size(sig), maxloc); % convert logical 

    %-------------------------------------------------------------------%
    % Count pixels through each column > half(max) value of that column
    %-------------------------------------------------------------------%

    newfilename = 'Results.csv'; % write new values to .csv files
    Array =  zeros(2592,2);
    % % R = Array(:,1);
    % y = Array(:,2);
    for a = 1: 2592% maxloc_row =  635 maxloc_col = 1094 
       [a_maxval, a_maxloc] = max(sig(:,a)); % search max among every column.
       % maxloc is the row at which maxval is.
       newval = a_maxval/2; % averaged max value
    % New structure for find width
             x = 0; 
             x = Array(:, 1);
             for b = 1: 1944 % maxloc_row =  635 maxloc_col = 1094
    %                R = b;
                if sig(b,a) > newval
                    x=x+1;
                end      
             end % End row search
              x;
    % y = x*(2.2); % pixels into microns
              output = [num2cell(x)];
             xlswrite(newfilename, output);
    end % End column search

1 个答案:

答案 0 :(得分:0)

我认为csvwrite可以将数据写入csv文件。以下是您可以尝试的内容:

output = [];置于顶部,在clc;

下面说

然后,替换

带有output = [num2cell(x)];

output = [output x];

xlswrite(newfilename, output);csvwrite(newfilename, output);但将其放在计划的最后,因为output内有x

我已经尝试过并且有效。