Dot indexing is not supported for variables of this type
Show older comments
Hi,
I am developping an app, where i want to load data from a .m fille to fill items for a drop down list. How ever in run into an error "Dot indexing is not supported for variables of this type". Any one knows how to fix this issue ? my code is bellow . Thank you
function startupFcn(app, varargin)
evalin('base', 'clear all')
evalin('base', 'clc')
list = fopen('VehicleList.m','r'); % Read data from the file
A = textscan(list, '%c'); % Importing data
fclose(list); % close file
AB = char(A); % Cell to char conversion
assignin('base','A', A); % send to workspace
assignin('base','AB', AB); % send to workspace
app.VehicleModelDropDown.Items = AB.VehicleList; % << I get the error here
function UpdateVehicleList(app,new_car)
VehicleList = [app.VehicleModelDropDown.Items new_car];
save('VehicleList.m','VehicleList','-append');
app.VehicleModelDropDown.Items = VehicleList;
end
end
13 Comments
AB has type char. What do you expect
AB.VehicleList
to do with a character type variable?
Why are you opening an M-file for reading?:
list = fopen('VehicleList.m','r');
(As opposed to running or calling it). That textscan useage is very ... unusual.
Why do you need evalin everywhere?
Mohammad Sami
on 31 Aug 2020
I suggested to save the file as .mat file. Not as.m. you can use the save and load functions with .mat file.
Khalala Mamouri
on 31 Aug 2020
Mohammad Sami
on 31 Aug 2020
If you want to read and load from plain text file use readmatrix and writematrix functions.
writematrix(VehicleList,'test.txt');
VehicleList = readmatrix('test.txt','OutputType','string');
Using load with the -ascii option requires that the file contains only one numeric matrix, as the load documentation clearly states: "ASCII files must contain a rectangular table of numbers, with an equal number of elements in each row."
Your file apparently contains other non-number characters, e.g. the text 'Nissan leaf'.
It seems likely that your file is actually some kind of text file which misleadingly has the extension .mat. But this is just a guess based on the few small clues you have given us so far. If you want any further help then please upload a sample file by clicking the paperclip button, so that someone can help you to import your file data correctly (e.g. using readtable).
Mohammad Sami
on 31 Aug 2020
Edited: Mohammad Sami
on 31 Aug 2020
function startupFcn(app, varargin)
VehicleList = readmatrix('vehiclelist.txt','OutputType','string');
app.VehicleModelDropDown.Items = VehicleList;
end
function UpdateVehicleList(app,new_car)
VehicleList = [app.VehicleModelDropDown.Items string(new_car)];
writematrix(VehicleList,'vehiclelist.txt');
app.VehicleModelDropDown.Items = VehicleList;
end
Khalala Mamouri
on 31 Aug 2020
Mohammad Sami
on 31 Aug 2020
Can you check that you have a variable named "VehicleList" in the ".mat" file.
Khalala Mamouri
on 31 Aug 2020
Khalala Mamouri
on 31 Aug 2020
Mohammad Sami
on 31 Aug 2020
Can you amend this part, Also check the txt file has one item per row.
function UpdateVehicleList(app,new_car)
VehicleList = [string(app.VehicleModelDropDown.Items) string(new_car)]';
writematrix(VehicleList,'vehiclelist.txt');
app.VehicleModelDropDown.Items = VehicleList;
end
Mohammad Sami
on 31 Aug 2020
Edited: Mohammad Sami
on 31 Aug 2020
Another option can be to use readcell and writecell functions.
function startupFcn(app, varargin)
VehicleList = readcell('vehiclelist.txt');
app.VehicleModelDropDown.Items = VehicleList;
end
function UpdateVehicleList(app,new_car)
VehicleList = [cellstr(app.VehicleModelDropDown.Items) cellstr(new_car)]';
writecell(VehicleList,'vehiclelist.txt');
app.VehicleModelDropDown.Items = VehicleList;
end
Khalala Mamouri
on 31 Aug 2020
Answers (0)
Categories
Find more on Text Files 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!
