Assignment A(:)=B error while extracting data from Excel
Show older comments
I've created the following code to extract data from known cell locations in a set of Excel spreadsheets. This can sometimes be up to 100 or more files. The code works well enough for most of the data collection, but faults out close to the end with the following error:
In an assignment A(:) = B, the number of elements in A and B must be
the same.
Error in ExcelCellExtraction (line 25)
ws_power(n) = xlsread(strcat(StartFolder,'\',filenames(n).name),'Test Section','C495');
Full code pasted below.
I'm only really interested in storing the data as a single-column data set for copying into statistical analysis software. I haven't tried hard-coding analysis because the requirements can be different for every type of data.
%%Excel Analysis %%
% %%This script is intended to analyze a set of Excel data in order to
% find data within a series of Excel files in the same directory. This will
% only function properly if the cell location is the same for all types.
% Additionally, basic mean and standard deviation is performed on the data.
% clear command window
clc
% clear all the current plots
close all
% clear all the current data
clear all
StartFolder = uigetdir('C:\','Choose File Directory'); %Standard call for
% Starting file location.
filenames = dir(StartFolder);
len = length(filenames);
start_power = zeros(len,1);
ws_power = zeros(len,1);
for n = 1:len;
if filenames(n).bytes > 1000;
start_power(n) = xlsread(strcat(StartFolder,'\',filenames(n).name),'Test Section','B377');
ws_power(n) = xlsread(strcat(StartFolder,'\',filenames(n).name),'Test Section','C495');
else
start_power(n) = 0;
ws_power(n) = 0;
end
end
Thanks!
Answers (1)
Walter Roberson
on 12 Sep 2017
0 votes
If C495 is not populated in the file, or is something that is not numeric, then xlsread() of it will return [] which cannot be stored in a numeric slot.
You should read into a variable and test the size of the variable before storing it in the vector.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!