From: Chris Rock on
Hi,

Another newbie question. I've imported some data in from an Excel file using the Import Wizard, and from this I've generated an M-file using the "Generate MATLAB code" tickbox.

I've then added further code in the M-file to perform some simple maths/analysis of the data in the variables that are created.

In these later sections of code I call different variables. When I initially did this with my test Excel file, it worked fine, but now I'm trying to use it on another Excel file, the says that the variable I've called is undefinied. When the importfile section of the code has completed and created the variables in the workspace, I've used the standard variable names it creates in the M-file, but it says that it's undefined. I'm confused and don't understand why, if it's already created the variable, that when the variable is called later on in the code it says it's undefined. Below is the code I've currently got written in the fiile:

function importfile1(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read

% Auto-generated by MATLAB on 05-Aug-2010 13:51:54

% Import the file
% Removed code: sheetName='04072010 1222' and removed 'sheetName' from Line
% 10
[numbers, strings, raw] = xlsread(fileToRead1);
if ~isempty(numbers)
newData1.data = numbers;
end

if ~isempty(strings) && ~isempty(numbers)
[strRows, strCols] = size(strings);
[numRows, numCols] = size(numbers);
likelyRow = size(raw,1) - numRows;
% Break the data up into a new structure with one field per column.
if strCols == numCols && likelyRow > 0 && strRows >= likelyRow
newData1.colheaders = strings(likelyRow, :);
end
end

% Create new variables in the base workspace from those fields.
for i = 1:size(newData1.colheaders, 2)
assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i));

end
%% Convert variable names - HERE I'VE TRIED TO CREATE NEW VARIABLES (VBatt, IBatt, etc) FROM THE ORIGINAL VARIABLES CREATED IN THE IMPORTFILE FUNCTION

VBatt = BatteryVoltage0x28Volt0x29;
IBatt = BatteryCurrent0x28Ampere0x29;
PMot = EngineLoad0x28KW0x29;
Speed = VehicleSpeed0x28KPH0x29;


%% Try adding data calculations to same M-file - IN THIS SECTION I'VE THEN CALLED THE NEW VARIABLES (SHORTENED NAMES) TO PERFORM THE CALCULATIONS. IF I TRY USING THE ORIGINAL VARIABLE NAMES IT STILL DOESN'T WORK, WHICH I DON'T UNDERSTAND.

PBatt = VBatt(:,:).*IBatt(:,:); % Generate battery power from V*I
EBatt = PBatt(:,:)./3600; % Generate battery energy total (Net)
EMot = PMot(:,:)./3600; % Generate motor energy


Any help would be very gratefully received!

Thanks,

Chris
From: Andy on
Well, without seeing the structure of your Excel files, we can't be sure. But I would guess this line is the problem:

assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i));

This uses the column headers of the Excel sheet as the variable names. If the column headers of the new Excel sheet are at all different (case matters!) then you're going to get different variable names. Try the following: first, type

>> dbstop if error

at the command line. Then run importfile1 on your new sheet. When the error occurs, MATLAB will stop and let you examine the variables directly. Check to make sure BatteryVoltage0x28Volt0x29, BatteryCurrent0x28Ampere0x29, etc. exist in the workspace.

That should explain the problem. But don't think that the solution is to change the column headers of all of your Excel files. A better solution, if you know, for example, that VBatt will always be your first column in your imported data, is to delete this section entirely:

for i = 1:size(newData1.colheaders, 2)
assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i));

end
VBatt = BatteryVoltage0x28Volt0x29;
IBatt = BatteryCurrent0x28Ampere0x29;
PMot = EngineLoad0x28KW0x29;
Speed = VehicleSpeed0x28KPH0x29;

And instead replace it with (something like):

VBatt = newData1.data(:,1);
IBatt = newData1.data(:,2);
PMot = newData1.data(:,3);
Speed = newData1.data(:,4);


There's no reason to use assignin if you're just going to rename your variables anyway. And this code will be much clearer to read later. (You won't have to wonder why the variable BatteryVoltage0x28Volt0x29 appeared out of thin air in your code.)
From: Jan Simon on
Dear Chris,

> % Create new variables in the base workspace from those fields.
> for i = 1:size(newData1.colheaders, 2)
> assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i));

If you assigne the variables in the base workspace, they cannot be accessed in the current work space, if you are in a function.
Is there a really good reason to use the fragile ASSIGNIN('base') ?

> VBatt = BatteryVoltage0x28Volt0x29;

The Variable "BatteryVoltage0x28Volt0x29" was created in the base workspace. In which workspace do you try to assign "VBatt"?

> %% Try adding data calculations to same M-file - IN THIS SECTION I'VE THEN CALLED THE NEW VARIABLES (SHORTENED NAMES) TO PERFORM THE CALCULATIONS. IF I TRY USING THE ORIGINAL VARIABLE NAMES IT STILL DOESN'T WORK, WHICH I DON'T UNDERSTAND.

"It still doesn't work" does not contain useful information. Do you get an error message, if so, which one and in which line? Do the results differ from your expectations? Did you go through your code line by line using the debugger to find out, where your problems appear?

Kind regards, Jan