Appending to an excel in matlab - matlab

I am following up on my own question. By using:
excelWorkbook.SaveAs('figtest.xlsx');
I am overwriting the existing excel. I have created a function which uses the code to save two images to excel. Now I want to iterate over a number of images, process each image and then save the result alongside the original image to an excel. Obviously calling the function each iteration is a bad idea since, each iteration is deleting the results of former iterations.
How can I add the current data to the excel file without deleting any former data?
Is there a better way to do this?
Note that the data is images and not simple numeric data.

Create a COM server once, iterate over the images and place them in the desired cells, quit the server and then delete the server object.
Building from my previous answer, here is a working example with 5 of many built-in images:
%Some sample images
im{1}=imread('peppers.png');
im{2}=imread('cameraman.tif');
im{3}=imread('pears.png');
im{4}=imread('greens.jpg');
im{5}=imread('bag.png');
excel = actxserver('Excel.Application'); % Create server object
excelWorkbook = excel.Workbooks.Add(1); % Add a workbook
excelSheet = excel.ActiveSheet; % Get the active sheet
f = figure('visible','off'); % To not show the images in MATLAB
dpi = get(groot, 'ScreenPixelsPerInch'); % Get screen dpi
for k=1:5
imshow(im{k});
print(gcf, sprintf('-r%d', dpi), ... % Print the figure at the screen resolution
'-clipboard', '-dbitmap'); % to the clipboard as a bitmap
%Adjust the cell names where you want to paste the images as desired
excelSheet.Range(['B',num2str(k)]).PasteSpecial();
%Unlocking aspect ratio to adjust height and width of the image according to the cell
excelSheet.Shapes.Item(k).LockAspectRatio='msoFalse';
excelSheet.Shapes.Item(k).Width=excelSheet.Range(['B',num2str(k)]).Width;
excelSheet.Shapes.Item(k).Height=excelSheet.Range(['B',num2str(k)]).Height;
end
excelWorkbook.SaveAs('figtest.xlsx'); % Save workbook to a file
excelWorkbook.Close(); % Close workbook
excel.Quit(); % Quit server
excel.delete(); % Delete server object
which gives:

One approach is to place each set of images in its own sheet, creating new sheets in the Excel file as needed. Here's some sample code, modified from my previous answer:
% Start COM server:
excel = actxserver('Excel.Application');
excelWorkbook = excel.Workbooks.Add(1);
excelWorksheets = excelWorkbook.Worksheets;
% Initializations:
dpi = get(groot, 'ScreenPixelsPerInch');
for iSheet = 1:nIterations % Based on how many image sets you process
% Get a blank sheet:
if (iSheet == 1)
excelSheet = excel.ActiveSheet;
else
excelSheet = excelWorksheets.Add([], excel.ActiveSheet, 1);
end
% Load, process, and plot your images here:
...
% Paste image into sheet and adjust cell size:
print(hFigure, sprintf('-r%d', dpi), '-clipboard', '-dbitmap');
excelSheet.Range('B2').PasteSpecial();
excelSheet.Range('B2').RowHeight = excelSheet.Shapes.Item(1).Height;
widthScale = excelSheet.Range('B2').ColumnWidth./...
excelSheet.Range('B2').Width;
excelSheet.Range('B2').ColumnWidth = excelSheet.Shapes.Item(1).Width.*widthScale;
end
% Save and close workbook and stop COM server:
excelWorkbook.SaveAs('figtest.xlsx');
excelWorkbook.Close();
excel.Quit();
excel.delete();

Adding data to an excel file is not well defined: creating new sheets or adding data to an existing one?
Therefore, you probably need the following methodology:
Open the existing excel file (to which you want to add data)
Add the new data to the opened excel file
Save this edited excel file

Just found this here:
xlApp = actxserver('Excel.Application');
xlApp.visible = 1;
%Open the the spreadsheet
xlworkbook = xlApp.Workbooks.Open('figtest.xlsx');
xlsheet = xlworkbook.ActiveSheet;
mydata=randn(1,3);
data=xlsread('figtest.xlsx');
%Determine last row
last=size(data,1);
newRange=last+1;
xlCurrRange = xlsheet.Range(['A', num2str(newRange),':C', num2str(newRange)]);
xlCurrRange.Value2 = mydata;
%Save and Close the Excel File
invoke(xlworkbook,'Save');
invoke(excelApp,'Quit');
delete(excelApp);
Also, you may want to try this script from the File Exchange.

Related

Batch process and name/save images in a separate folder

I am running a script to create masks of 41 Controls, both Positive and Negative, for a total of 82 images. I don't know how to save the masks with names similar to the original .png files, such as "Masked Control_1 Positive", "Masked Control_1 Negative", etc to Control_41. I also don't know how to save these to a separate folder within my current folder. Thank you!
I have also included a screenshot of the current directory.
% GlassBrains_Controls2Mask.m
clear;
Files = dir('Control_*');
Controls_Results_Structure = struct('BW',[],'maskedRGBImage',[]);
for i = 1:length(Files)
RGB = imread(Files(i).name);
[Output_BW, Output_maskedRGBImage] = GlassBrainMask(RGB);
Controls_Results_Structure(i).BW = Output_BW;
Controls_Results_Structure(i).maskedRGBImage = Output_maskedRGBImage;
end
save("Controls_Results_Structure")
% Save the results
mkdir "Masked Glass Brains (Controls)"
for i = 1:length(Files)
Image_Number = i;
save(Controls_Results_Structure(Image_Number).BW);
end
Please notice that save() stores workspace variables in MAT files, which are generally meant to be opened with MATLAB only, not with image viewers and such. PNG files like those you are reading with imread() can be created with imwrite():
% Save the results
outdir='./Masked Glass Brains (Controls)';
[mkdir_status,~,~]=mkdir(outdir);
assert(mkdir_status,'Error creating output folder.');
clear mkdir_status;
for i=1:length(Files)
imwrite( Controls_Results_Structure(i).BW, ...
sprintf('%s/Masked %s',outdir,Files(i).name) ... %alternative: [outdir '/Masked ' Files(i).name]
);
end
To customize the image file that imwrite() creates from the output of GlassBrainMask(), see: https://www.mathworks.com/help/matlab/ref/imwrite.html

How to plot different files information in a different graph using a loop

I have a program in octave that read a file, then some functions takes different parts of that file to make some plots. But now I want to read many files and creates a plot for each file. The program that I have, read these files, but at the moment of plotting, it creates multiple plots, but with the same data file (first data file).
I already try using hold on and hold off, but it did not work.
myfiles = dir('*.lvm'); % To load every file of .lvm
%%% - To read files
for i = 1:length(myfiles) % Loop with the number of files
files = myfiles(i).name; % Structure element with file's names
n(:,:,i)= dlmread(files,'',2,1);% To read the contain
CM1N1 = n(:,2,:);
CM2N1 = n(:,3,:);
CM3N1 = n(:,4,:);
CM4N1 = n(:,5,:);
N1m(:,:,:) = [CM1N1 CM2N1 CM3N1 CM4N1];
%========================================================================%
% Calling all functions
%========================================================================%
%--- Function config_horizontale which plot X position and has the respective info
[Nmean1m]=config_horizontale(n,N1m);
end
'''
AND THIS IS A PART OF THE FUNCTION
''' function [Nmean1m]=config_horizontale(n,N1m)
%=========================================================================%
% To plot
%=========================================================================%
Nmean1m = mean(N1m);
xM = 20:20:80;
i=1;
figure
plot(xM,Nmean1m(:,:,i),'-+')
end
As 'i' is the number of files, I need,first to plot the first file, then the second one and so on and on. Thanks in advance.
Here you will find an example of the file
LabVIEW Measurement
-0.004110 0.003667 -0.007486 0.003185 -0.005198 -0.005292
-0.001542 0.001109 -0.002435 0.003185 -0.005198 0.003184
-0.006678 0.006224 -0.002435 0.000531 -0.010615 0.000358
-0.006678 -0.001448 -0.007486 0.005839 -0.005198 -0.002467
-0.006678 0.003667 -0.004961 0.005839 -0.007906 -0.002467
-0.006678 0.003667 -0.002435 0.008493 -0.007906 0.003184

store multi images in mat file using matlab

The bellow matlab code is to read 6 images from folder and save them in mat file
then return to read the mat file and check the images inside it
the problem is that
just the last images stores in mat file
the problem is in save function::
what should I edit to make save function store all images that stored in result cell into mat file
%Generate mat file
srcFile = dir('C:\Users\Desktop\images\*.jpg');
result = cell(1,length(srcFile));
for i = 1 : length(srcFile)
filename = strcat('C:\Users\Desktop\images\',srcFile(i).name);
I = imread(filename);
%figure, imshow(I);
I = imresize(I,[128 128]);
result{i} = I;
figure, imshow(result{i});
end
save images, result;
%Read mat file
for j =1 :length(srcFile)
filename = strcat('C:\Users\Desktop\images\',srcFile(j).name);
I = imread(filename);
a='I';
input = load('images.mat',a);
figure, imshow(input.(a));
end
Your first loop, up to the save, is fine.
When you load the data, use load('images.mat') before the second loop. Then, you have back the result variable in your workspace and you iterate on it:
load('images.mat')
for j = 1:length(srcFile)
figure, imshow(result{j});
end
What you have to remember is that your .mat file only contains the variables you saved, but you can't access them directly with load. You first load them, and then access the loaded variables (which have a different name from the file, usually).
Finally, if you want to check this code, you need to clear the workspace after the save, otherwise you may not notice that some of the variables you use are not there anymore (like the error you have got with I).

Importing large amount of data into MATLAB?

I have a text file that is ~80MB. It has 2 cols and around 6e6 rows. I would like to import the data into MATLAB, but it is too much data to do with the load function. I have been playing around with the fopen function but cant get anything to work.
Ideally I would like to take the first col of data and import and eventually have it in one large array in MATLAB. If that isn't possible, I would like to split it into arrays of 34,013 in length. I would also like to do the same for the 2nd col of data.
fileID = fopen('yourfilename.txt');
formatSpec = '%f %f';
while ~feof(fileID)
C = textscan(fileID,formatSpec,34013);
end
Hope this helps..
Edit:
The reason you are getting error is because C has two columns. So you need to take the columns individually and handle them.
For example:
column1data = reshape(C(:,1),301,113);
column2data = reshape(C(:,2),301,113);
You may also consider to convert your file to binary format if your data file is not changing each time you want to load it. Then you'll load it way much faster.
Or you may do "transparent binary conversion" like in the function below. Only first time you load the data will be slow. All subsequent will be fast.
function Data = ReadTextFile(FileName,NColumns)
MatFileName = sprintf('%s.mat',FileName); % binary file name
if exist(MatFileName,'file')==2 % if it exists
S = load(MatFileName,'Data'); % load it instead of
Data = S.Data; % the original text file
return;
end
fh = fopen(FileName); % if binary file does not exist load data ftom the original text file
fh_closer = onCleanup( #() fclose(fh) ); % the file will be closed properly even in case of error
Data = fscanf(fh, repmat('%f ',1,NColumns), [NColumns,inf]);
Data = Data';
save(MatFileName,'Data'); % and make binary "chache" of the original data for faster subsequent reading
end
Do not forget to remove the MAT file when the original data file is changed.

Delete Excel Figures from Matlab

I would like to directly delete all the figures in an excel file from MATLAB. I can select all the figures using activex but I cant' figure out a way to delete them.
My code:
filename_out = 'Libraries\Documents\TEST.xlsx'; % filename
Excel = actxserver('Excel.Application'); % open the connection
set(Excel,'Visible',1);
Excel.Workbooks.Open(filename_out); % open excel file
worksheets = Excel.sheets;
numSheets = worksheets.Count; % count the number of sheets
for s = 1 : numSheets % do a loop for all sheets
worksheets.Item(s).Shapes.SelectAll; % select the figure
% How to delete selection? *
end
Thanks for any help!
Within your loop, do something like
myshapes = worksheets.Item(s).Shapes;
for j = myshapes.Count:-1:1
myshapes.Item(j).Delete
end
Note that we're counting down from myshapes.Count to 1, as the count goes down each time you remove one.