Create an empty array of size (MxN) for xlswrite.

조회 수: 2 (최근 30일)
Abhilash Sukumari
Abhilash Sukumari 2015년 6월 22일
답변: Image Analyst 2015년 6월 22일
I have an Image which is 1944® x 2592 (C). I would like to take one column at a time; treating each column as an Image and calculate how many pixels among each row of that column contains value > half(max) value of that column. Those number of pixels are to be written to the excel sheet corresponding to it's respective column.
Image: Image
Here is what I have tried so far, I am not able to write it successfully.
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,2592);
% % 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

답변 (4개)

James Tursa
James Tursa 2015년 6월 22일
편집: James Tursa 2015년 6월 22일
If an array has size 2592 x 2, then it is not empty. An empty array has at least one dimension that has size 0. If you mean create an array with zeros, then
Array = zeros(2592,2);
  댓글 수: 1
Abhilash Sukumari
Abhilash Sukumari 2015년 6월 22일
No, I just want to initialize an array with dimensions: 2592 x 2

댓글을 달려면 로그인하십시오.


Chad Greene
Chad Greene 2015년 6월 22일
Try
Array = NaN(2592,2) ;
  댓글 수: 1
Abhilash Sukumari
Abhilash Sukumari 2015년 6월 22일
I am later using xls write to write values into the array created by NaN, it would only write NaN. That is my problem. Let me edit the question to what I am trying to do.

댓글을 달려면 로그인하십시오.


Jeremy
Jeremy 2015년 6월 22일
If you are writing to excel then you probably want to use a cell array,
Array = cell(2592,2);
  댓글 수: 1
Abhilash Sukumari
Abhilash Sukumari 2015년 6월 22일
Please have a look at the script. I am trying to write the x values to the excel. However, the final out in excel seems to show wrong with only one constant value in all 2592 rows.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 6월 22일
I'm surprised this even finished! You definitely don't want to call xlswrite 2592 times or it will take hours. Perhaps you opened up Excel to look at it after only a few of the calls to xlswrite had been executed.
You should create a 2D matrix for output and then put a single call to xlswrite after both "a" and "b" loops have finished.
Other problems: your (badly-named) x is just column 1 of Array every time. And you don't even need the loop over b because you can use the sum function: sum(x>newval) to count the number of pixels more than the half max value (which you chose the poor, non-descriptive name of newval for).

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by