Dot indexing is not supported for variables of this type

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?
I suggested to save the file as .mat file. Not as.m. you can use the save and load functions with .mat file.
Ok; lets do it with load same code as before:
  • what do i expect for type char ? >> I am not sure i understand you question ... Anyways, i want to set a list of items (Name of vehicules) and i want then to be loaded to the drop down list.
  • Why are you opening an M-file for reading?: >> Ok let's use an .mat file then and lets get rid of the textscan thing
list = load('VehicleList.mat','-ascii');
assignin('base','list', list);
app.VehicleModelDropDown.Items = list.VehicleList;
function UpdateVehicleList(app,new_car)
VehicleList = [app.VehicleModelDropDown.Items new_car];
save('VehicleList.mat','VehicleList','-append');
app.VehicleModelDropDown.Items = VehicleList;
end
here is the error i get now :
Error using load
Unknown text on line number 1 of ASCII file VehicleList.mat
"Nissan leaf".
any potential answers to that ?
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).
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
Unfortunately matlab does not allow me to upload the file since it is an app file.
Anyways, yes you where right ! i saved the .mat file in the wrong way and matlab does show me that it is an .map file but in reality it is .m file .
So this is the Updated code with a screen shot , that may help you to debug better the problem. Now load works just fine and loads the data perfectly, however Now i get this error : Reference to non-existent field 'VehicleList'.
It is because matlab can't find the name VehicleList i guess?
function startupFcn(app, varargin)
evalin('base', 'clear all')
evalin('base', 'clc')
list = load('VehicleList.mat');
assignin('base','list', list);
app.VehicleModelDropDown.Items = list.VehicleList; % Reference to non-existent field 'VehicleList'.
function UpdateVehicleList(app,new_car)
VehicleList = [app.VehicleModelDropDown.Items new_car];
save('VehicleList.mat','VehicleList','-append');
app.VehicleModelDropDown.Items = VehicleList;
end
Can you check that you have a variable named "VehicleList" in the ".mat" file.
@mohammed your answer is correct !! works just fine with your last code ! thank you so much
VehicleList = readmatrix('vehicleList.txt','OutputType','string');
app.VehicleModelDropDown.Items = VehicleList;
function UpdateVehicleList(app,new_car)
VehicleList = [app.VehicleModelDropDown.Items string(new_car)];
writematrix(VehicleList,'vehicleList.txt');
app.VehicleModelDropDown.Items = VehicleList;
end
This is my last question i promess haha !
Actualy when i putt more than one car type this error appears :
Error using matlab.ui.control.internal.model.AbstractStateComponent/set.Items (line 184)
Expected input to be a vector.
any solutions please .?
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
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
it works just fine . Wow you are amazing man !

Sign in to comment.

Answers (0)

Products

Release

R2020a

Asked:

on 31 Aug 2020

Commented:

on 31 Aug 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!