xlsread in matlab app designer
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stetson Spencer
el 21 de Nov. de 2018
Editada: Cris LaPierre
el 21 de Nov. de 2018
Hi I am trying to do xlsread in my matlab app designer. All I did was copy and paste:
series1=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',1)
series2=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',3)
series3=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',5)
rinse1=series1(18:26,1);
rinse2=series2(15:23,1);
rinse3=series3(15:23,1);
dM1=series1(18:26,2:end);
dM2=series2(15:23,2:end);
dM3=series3(15:23,2:end);
But it's giving me an error message: Invalid default value for property 'rinse1' in class 'descaling_app':
Undefined function 'series1' for input arguments of type 'double'.
3 comentarios
Respuesta aceptada
Cris LaPierre
el 21 de Nov. de 2018
Editada: Cris LaPierre
el 21 de Nov. de 2018
Under properties? That's the issue. I can now duplicate your error message.
Properties is for defining variables. If you want to run code, add it to a function. It can be a callback related to a button press or a helper function. If this is how you want things to start, consider placing it in the startupFcn Callback.
properties (Access = private)
series1;
rinse1;
dM1;
end
...
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
app.series1=xlsread('test.xlsx',1)
app.rinse1=app.series1(18:26,1);
app.dM1=app.series1(18:26,2:end);
end
end
If the variables are declared in Properties, they exist on the app structure and are passed into all functions. If you don't need them to be available outside the function, you could also just declare them within the function itself.
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
series1=xlsread('test.xlsx',1)
rinse1=series1(18:26,1);
dM1=series1(18:26,2:end);
end
end
Note that the code here executes in response to a button push in the app, so is added to the ButtonPushed callback function.
0 comentarios
Más respuestas (1)
Guillaume
el 21 de Nov. de 2018
Editada: Guillaume
el 21 de Nov. de 2018
You probably need to learn a bit more about class design in matlab. Indeed, the code you have written cannot be put in the properties section of the class definition. You are in effect declaring the class properties with some default values, but as documented, expressions for default values cannot reference other variables.
A better design would be to have, in the properties section, just:
properties (Access = private)
series1;
series2;
series3;
rinse1;
rinse2;
rinse3;
dM1;
dM2;
dM3;
end
function startupFcn(app)
app.series1=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',1)
app.series2=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',3)
app.series3=xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx',5)
app.rinse1=series1(18:26,1);
app.rinse2=series2(15:23,1);
app.rinse3=series3(15:23,1);
app.dM1=series1(18:26,2:end);
app.dM2=series2(15:23,2:end);
app.dM3=series3(15:23,2:end);
end
And since numbered variables are a bad design, even better would be:
properties (Access = private)
series;
rinse;
dM;
end
function startupFcn(app)
%constants
sheets = [1; 3; 5]
ranges = [18 26; 15 23; 15 23];
%initialisation
app.series = cell(size(sheets));
app.rinse = cell(size(sheets));
app.dM = cell(size(sheets))
%loading
for i = 1:numel(sheets)
app.series{i} = xlsread('Copy of Shaker Table Descaling Spreadsheet v10292018.xlsx', sheets(i))
app.rinse{i} = series{i}(ranges(i, 1):ranges(i, 2), 1);
app.dM{i} = series{i}(ranges(i, 1):ranges(i, 2), 2:end);
end
end
This latter approach has the advantage that if you want to add series, you don't have to modify the code, just edit the two constants sheets and ranges.
0 comentarios
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!