Storing Results in a Matrix

18 views (last 30 days)
Emerson Nithiyaraj
Emerson Nithiyaraj on 10 Dec 2019
Answered: Image Analyst on 23 Dec 2019
hy
I am working in medical image processing with DICOM file. I have a code which produces '8 output variables' finally after processing 'n' no of slices. Say Patient 1 has 500 slices, after processing '500' slices the output will be as below.d1.png
So if I want to store the 8 output variable's results similarily for 10 patients in this same matrix 'res1', (i.e. My matrix should have 10 rows - 10 rows for 10 patients) how should i code?
Please help !

Answers (2)

Bhargavi Maganuru
Bhargavi Maganuru on 23 Dec 2019
Edited: Bhargavi Maganuru on 23 Dec 2019
You could use cell array to store the data.
c={}; %Create an empty cell array
Add your outputs to the cell array by assignment. For example, the first row in your output can be added as
c(1,:)={'E:\PHD\',501,100};
You could use cell2table to convert cell to table, if column names need to be stored.
c=cell2table(c,'VariableNames',{'Name','Total slices','No of Slices'});
  1 Comment
Stephen23
Stephen23 on 23 Dec 2019
Edited: Stephen23 on 23 Dec 2019
"For example, the first row in your output can be added as c(1,:)=['E:\PHD\',501,100]"
The square brackets are a concatentation operator, and MATLAB implicitly converts the numeric scalars to the class of the left-most array being concatenated, as explained here:
which in this case is a character array. So this code:
['E:\PHD\',501,100]
is equivalent to
['E:\PHD\',char([501,100])]
which is equivalent to
'E:\PHD\ǵd'
This character vector is then assigned to the cell array using parentheses:
c(1,:) = ...
which MATLAB implicitly converts to curly braces and allocates that character vector as the content of the first cell in the first row, expanding the cell array as required. Note that the colon is totally superfluous as the cell array only has one column, although it does imply an attempt to assign to multiple columns... which it would be if curly braces were used:
c(1,:) = {'E:\PHD\',501,100}
Even better would be to preallocate the cell array to the correct size:

Sign in to comment.


Image Analyst
Image Analyst on 23 Dec 2019
I do this all the time. I make up an Excel template in advance with the right column headers. You can make it as fancy as you wish, for example make row 1 (the column headers row) auto-sized with bold text, and a thick bottom border. Perhaps with yellow highlighting or whatever you want. Make it look exactly like you'd like it to look.
Then process all your files and keep track of the filenames in a cell array, and the numerical results in a 2-D double array. Then, when you're done processing all the dicom files, use copyfile() to copy the template to the final filename that you'd like for your results, then call xlswrite() to transfer the filenames to cell A2, and the numerical results to cell B2. You can store the numerical results in the same cell array as the filenames, but it gets a bit kludgy to transfer the numerical results to it - easier to just append on an additional row to a array you build up. Like in the loop over files
filePattern = fullfile(folder, '*.dcm'); % Folder is wherever your images live, or pwd if they're in the current folder.
files = dir(filePattern);
allFileNames = {files.name} % Get all filenames into a single cell array.
allResults = zeros(numberOfFiles, 8); % Create a master array for all the results from all images.
for k = 1 : numberOfFiles % Process each file
thisFileName = fullfile(folder, filenames{k}); % Get the name of this file.
theseResults = ProcessSingleDicomFile(thisFileName) % Get your 8 result numbers.
allResults(k, :) = theseResults; % Store these results in our master array.
end
% Now make an excel workbook from our template.
excelFileName = fullfile(pwd, 'Results.xlsx'); % Whatever....
excelTemplateFilename = fullfile(pwd, 'ResultsTemplate.xlsx'); % Whatever....
if isfile(excelTemplateFilename)
copyfile(excelTemplateFilename, excelFileName)
end
% Now transfer the data to the 'Results' sheet of our output workbook.
xlswrite(excelFileName, filenames, 'Results', 'A2')
xlswrite(excelFileName, allResults, 'Results', 'B2')
Of course, you need to write the function (which you already have) called ProcessSingleDicomFile() that processes just a single file and returns the 8 numerical results. No need to return that first row of your cell array, which contains column headers, for every image since it's the same for all of them.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!