I have a user input function to extract 3 rows of data from a table and assigned this data to the variable 'all_sectors'. When I try to use this variable to plot the data on a bar chart it comes up with the following error message:
Unrecognized function or variable 'all_sectors'.
Error in train_fares_script (line 36)
y = all_sectors ;
My code to select the data is:
if year == 2004
all_sectors = readmatrix("train_fares.xlsx",'sheet','2004','range','A1:A3')
elseif year == 2005
year = 2005
all_sectors = readmatrix("train_fares.xlsx",'sheet','2005','range','A1:A3')
My code to plot this on a bar graph is:
x = categorical({'London and South East','Long Distance','Regional'});
y = all_sectors ;
bar(x,y)
How can I fix this so the variable is recognised?
Ive tried copying and pasting the variable name to ensure it is spelled correctly and changed the name to see if that would work but it hasn't.
Related
I have two editable numeric fields and a table in app designer; the user can enter the values in these editable fields and then push a button. Then, the values are added to a table. Also, I provide an option to attach an excel folder that should have two columns to reflect on the table.
Both of these work perfectly fine individually, but, if I added the values manually then attached an excel folder or vice versa, I get the following error: All tables in the bracketed expression must have the same variable names.
The function that handles the editable fields:
app.t = app.UITable.Data;
x = app.xvalueEditField.Value;
y = app.yvalueEditField.Value;
nr = table(x, y);
app.UITable.Data = [app.t; nr]; %% error happens here if I attach excel then add manually
app.t = app.UITable.Data;
The Function of the excel folder:
text = readtable([pathname filename], "Sheet",1, 'ReadVariableNames',false);
fl = cellfun(#isnumeric,table2cell(text(1,:)));
if (numel(fl(fl == false)) > 0)
flag = false;
else
flag = true;
end
if (flag)
A = [app.t; text]; %% error happens here if I add manually then attach
app.UITable.Data = A;
app.t = text;
end
Note: these are only the parts of the function, where I attempt to combine values
Can someone please help me?
Thank you
The error message is telling you that table only allows you to vertically concatenate tables when the 'VariableNames' properties match. This is documented here: https://www.mathworks.com/help/matlab/ref/vertcat.html#btxzag0-1 .
In your first code example, the table nr will have variable names x and y (derived from the names of the underlying variables you used to construct the table). You could fix that case by doing:
% force nr to have the same VariableNames as app.t:
nr = table(x, y, 'VariableNames', app.t.Properties.VariableNames);
and in the second case, you can force text to have the correct variable names like this:
text.Properties.VariableNames = app.t.Properties.VariableNames
I'm trying to create State block from sflib (stateflow library) :
chart = add_block('sflib/Chart', 'autoDS/Chart'); %Create Chart Block
state = Stateflow.State(chart); %Create State in Chart Block
state.Name = 'CMD_28V_Avion';
open_system('autoDS');
It returns me "No constructor 'Stateflow.State' with matching signature found" error.
How can i fix it please ? I don't know if it's a syntax or a library error as i saw on many post.
add_block returns a handle to the added block (which for a state chart is actually a subsystem.) It does not return a handle to a chart, which is what Stateflow.State requires as its input. Hence the error.
You need,
add_block('sflib/Chart', 'autoDS/Chart'); %Create Chart Block
rt = sfroot;
m = rt.find('-isa', 'Simulink.BlockDiagram', '-and', 'Name', 'autoDS'); % block diagram
ch = m.find('-isa','Stateflow.Chart', '-and', 'Name', 'Chart'); % chart
st = Stateflow.State(ch); % state
st.Name = 'CMD_28V_Avion';
open_system('autoDS');
See the Access the Model Object and Access the Chart Object sections of Create Charts by Using the Stateflow API
I am trying to read in a whole set of environmental input/output data from the WIOD (World Input output database) through a nested loop over the countries and years. Now I have done something similar for the base/data accounts before. Now I try to load in the environmental data. My previously working code looked as follows:
for yr = 95:99
V(:,:,yr-94) = xlsread(['wiot' num2str(yr) '_row_apr12.xlsx'],['WIOT_19'
num2str(yr)],'E1443:BCI1448');
end
Now my code which is not working with the error message "File name must be a character vector." looks as follows:
country =
{'AUS','AUT','BEL','BGR','BRA','CAN','CHN','CYP','CZE','DEU','DNK','ESP',...
'EST','FIN','FRA','GBR','GRC','HUN','IDN','IND','IRL','ITA','JPN','KOR',...
'LTU','LUX','LVA','MEX','MLT','NLD','POL','PRT','ROU','RUS','SVK','SVN',...
'SWE','TUR','TWN','USA','ROW'}
for c = 1:41
for year = 1995:1995
F_NRG(:,(c*35)-34:(c*35),year-1994) = transpose(xlsread([country(c)
'_EU_May12.xlsm'],[num2str(year)],'AD2:AD36'));
end
end
I do not get it because the filename should be a string if I select the country c via country(c)? The xlsread is nested in a transpose command and the cells where I want to save the read in data are computed slightly more complicated but principally it should be the same? The following code also does render a string for each c.
for c = 1:41
country(c)
end
Can you help me find my coding mistakes? Why doesn't Matlab recognize the file name as a string?
Thank you for your help.
Try this,
for c = 1:41
for year = 1995:1995
F_NRG(:,(c*35)-34:(c*35),year-1994) = transpose(xlsread([country{c}
'_EU_May12.xlsm'],[num2str(year)],'AD2:AD36'));
end
end
I wrote a function, wins_plot, to read the scoreboard from a file and store the player's name, number of plays, wins, & losses. I stored all those using struct. I loop over the file, store each line in line, textscan for everything I need from line, and then iterate i (initially == 1) as I go to expand my array of structures. A snippet from the code to represent what I am saying:
c = textscan(line, '%s %s %d %d %d');
player(i).firstName = c{1};
player(i).lastName = c{2};
player(i).plays = c{3};
player(i).wins = c{4};
player(i).losses = c{5};
After all the file has been scanned and stored, I then write this code to extract the number of wins of each player and store it in X and then finally use the pie function to represent the values in X
for n=1:(i-1)
X(n) = player(n).wins;
end
pie(X);
I get a wall of error after:
Undefined function 'cos' for input arguments of type 'int32'.
Error in pol2cart (line 22) x = r.*cos(th);
Error in pie (line 99)
[xtext,ytext] = pol2cart(theta0 + x(i)*pi,1.2);
Error in wins_plot (line 30) pie(X);
I have no clue what might be wrong. Any help would be greatly appreciated. Please keep in mind that I only just started learning MATLAB today so my knowledge of it is very limited (and I have R2013a). Thank you in advance!
The numbers got read as int32, but when you call pie, it requires them to be double to do the computation. So, when you call pie, try casting the values to double. Try this,
pie(double(X));
I have many large dataset arrays in my workspace (loaded from a .mat file).
A minimal working example is like this
>> disp(old_ds)
Date firm1 firm2 firm3 firm4
734692 880,0 102,1 32,7 204,2
734695 880,0 102,0 30,9 196,4
734696 880,0 100,0 30,9 200,2
734697 880,0 101,4 30,9 200,2
734698 880,0 100,8 30,9 202,2
where the first row (with the strings) already are headers in the dataset, that is they are already displayed if I run old_ds.Properties.VarNames.
I'm wondering whether there is an easy and/or fast way to make the first column as ObsNames.
As a first approach, I've thought of "exporting" the data matrix (columns 2 to 5, in the example), the vector of dates and then creating a new dataset where the rows have names.
Namely:
>> mat = double(old_ds(:,2:5)); % taking the data, making it a matrix array
>> head = old_ds.Properties.VarNames % saving headers
>> head(1,1) = []; % getting rid of 'Date' from head
>> dates = dataset2cell(old_ds(:,1)); % taking dates as column cell array
>> dates(1) = []; % getting rid of 'Date' from dates
>> new_ds = mat2dataset(mat,'VarNames',head,'ObsNames',dates);
Apart from the fact that the last line returns the following error, ...
Error using setobsnames (line 25)
NEWNAMES must be a nonempty string or a cell array of nonempty strings.
Error in dataset (line 377)
a = setobsnames(a,obsnamesArg);
Error in mat2dataset (line 75)
d = dataset(vars{:},args{:});
...I would have found a solution, then created a function (such to generalize the process for all 22 dataset arrays that I have) and then run the function 22 times (once for each dataset array).
To put things into perspective, each dataset has 7660 rows and a number of columns that ranges from 2 to 1320.
I have no idea about how I could (and if I could) make the dataset directly "eat" the first column as ObsNames.
Can anyone give me a hint?
EDIT: attached a sample file.
Actually it should be quite easy (but the fact that I'm reading your question means that having the same problem, I first googled it before looking up the documentation... ;)
When loading the dataset, use the following command (adjusted to your case of course):
cell_dat{1} = dataset('File', 'YourDataFile.csv', 'Delimiter', ';',...
'ReadObsNames', true);
The 'ReadObsNames' default is false. It takes the header of the first column and saves it in the file or range as the name of the first dimension in A.Properties.DimNames.
(see the Documentation, Section: "Name/value pairs available when using text files or Excel spreadsheets as inputs")
I can't download your sample file, but if you haven't yet solved the problem otherwise, just try the suggested solution and tell if it works. Glad if I could help.
You are almost there, the error message you got is basically saying that Obsname have to be strings. In your case the 'dates' variable is cell array containing doubles. So you just need to convert them to string.
mat = double(piHU(:,2:end)); % taking the data, making it a matrix array
head = piHU.Properties.VarNames % saving headers
head(1) = []; % getting rid of 'Date' from head
dates = dataset2cell(piHU(:,1)); % taking dates as column cell array, here dates are of type double. try typing on the command window class(dates{2}), you can see the output is double.
dates(1) = []; % getting rid of 'Date' from dates
dates_str=cellfun(#(s) num2str(s),dates,'UniformOutput',false); % convert dates to string, now try typing class(dates_str{2}), the output should be char
new_ds = mat2dataset(mat,'VarNames',head,'ObsNames',dates_str); % construct new dataset.