Can you help me with an error on my GUI program ?

1 view (last 30 days)
Here are the errors :
Error using importdata (line 136)
Unable to open file.
Error in essai>initilisationMetrologie (line 181)
tempCart = importdata(nameFiles{ii})
Error in essai>essai_OpeningFcn (line 57)
Metro=initilisationMetrologie;
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in essai (line 42)
gui_mainfcn(gui_State, varargin{:});
Here is my code :
function varargout = essai(varargin)
% ESSAI MATLAB code for essai.fig
% ESSAI, by itself, creates a new ESSAI or raises the existing
% singleton*.
% H = ESSAI returns the handle to a new ESSAI or the handle to
% the existing singleton*.
%
% ESSAI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ESSAI.M with the given input arguments.
%
% ESSAI('Property','Value',...) creates a new ESSAI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before essai_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to essai_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help essai
% Last Modified by GUIDE v2.5 28-Jun-2017 17:39:43
% % Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @essai_OpeningFcn, ...
'gui_OutputFcn', @essai_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% % End initialization code - DO NOT EDIT
%
% %% --- Executes just before essai is made visible.
function essai_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to essai (see VARARGIN)
%
% Choose default command line output for essai
handles.output = hObject;
Metro = initilisationMetrologie;,
set(handles.Choix_Plan,'String',Metro.DataName) %Affectation des differents plan de Metrologie
handles.MetroData=Metro.dataCartesian;
handles.MetroDataPol=Metro.dataPol;
handles.MetroAllNames=Metro.allNames;
handles.MetroDataPolCoef = Metro.PolDataCoef;
handles.dataCartesian = Metro.dataCartesian;
guidata(hObject, handles);
% UIWAIT makes essai wait for user response (see UIRESUME)
uiwait(handles.figure1);
%
%% --- Outputs from this function are returned to the command line.
function varargout = essai_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%% --- Executes on selection change in Choix_Plan.
function Choix_Plan_Callback(hObject, eventdata, handles)
% hObject handle to Choix_Plan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns Choix_Plan contents as cell array
% contents{get(hObject,'Value')} returns selected item from Choix_Plan
contents = cellstr(get(hObject,'String'));
% disp('passage dans la function selection')
% disp(['tu as sectionné ',contents{get(hObject,'Value')}] )
%% --- Executes during object creation, after setting all properties.
function Choix_Plan_CreateFcn(hObject, eventdata, handles)
% hObject handle to Choix_Plan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%% --- Executes when selected object is changed in uipanel2.
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel2
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
contents = cellstr(get(handles.Choix_Plan,'String'));
if strcmp(get(eventdata.NewValue,'string'),'Couronne et Stack')
set(handles.uipanel6,'Visible','on')
else
set(handles.uipanel6,'Visible','off')
set(handles.uipanelOption,'Visible','off')
end
% disp ( ['tu appliques la fonction' , get(eventdata.NewValue,'string'), 'sur les données ' , contents{get(hObject,'Value')}])
%% --- initial Execute
function Metro=initilisationMetrologie()
%% préparation lancement script
%-------------------------------------------------------------------------%
%
%addpath(genpath('C:\Users\p101859\Documents\Matlab\[01] Essais val méca\[04] Matlab\[00] Macros pour traitement'))
%addpath(genpath('E:\calcul_Metrologie\c Matlab\c .1. Macros basiques'))
addpath(genpath('C:\Users\p116122\OneDrive - Alliance\Bureau\Codes MATLAB_Analyse_DATA_CETIM\Codes MATLAB_Analyse_DATA_CETIM'))
% Get all files, including Thumbs if we can see hidden files.
allFiles = dir (pwd);
allNames = {allFiles.name};
allFiles = allFiles(~strcmpi(allNames, 'Thumbs.db'));
allFiles = allFiles(~strcmpi(allNames, 'noms_phases.txt'));
clear allNames
allNames = {allFiles.name};
%% donées d'entrée
%-------------------------------------------------------------------------%
% recherche du numero de plans de métrologie
p = path;
path(p,allNames{3})
newFiles = dir(allNames{3});
nameFiles = {newFiles.name};
m = length(nameFiles)-2;
clear newFiles nameFiles p
dataPol = cell(length(allNames)-2,m);
dataCartesian = cell(length(allNames)-2,m);
PolDataCoef = cell(length(allNames)-2,m);
% chargement des données
% for i = 3 : length(allNames)
% p = path;
% path(p,allNames{i})
% newFiles = dir(allNames{i});
% nameFiles = {newFiles.name};
% for ii = 3:length(newFiles)
% tempCart = importdata(nameFiles{ii});
% % convertir les coordonnées cartessiennes en polaires
% [tempThetaDegres,rho] = cart2compass(tempCart(:,1),tempCart(:,3));
% rho = rho-3; % 6 = diamètre de bille du bras romer
% [tempX, tempY]=compass2cart(tempThetaDegres,rho);
% dataPol{i-2,ii-2} = [tempThetaDegres,rho];
% dataCartesian{i-2,ii-2} = [tempX, tempY];
% clear tempX tempY tempTheta tempR tempCart
% end
% path(p)
% end
for i = 3 : length(allNames)
p = path;
path(p,allNames{i})
newFiles = dir(allNames{i});
nameFiles = {newFiles.name};
for ii = 3:length(newFiles)
tempCart = importdata(nameFiles{ii})
% convertir les coordonnées cartessiennes en polaires
[tempThetaDegres,rho] = cart2compass(tempCart(:,1),tempCart(:,3));
[tempX, tempY]=compass2cart(tempThetaDegres,rho);
dataPol{i-2,ii-2} = [tempThetaDegres,rho];
dataCartesian{i-2,ii-2} = [tempX, tempY];
clear tempX tempY tempTheta tempR tempCart
end
path(p)
end
clear i ii
tic
clear newFiles allFiles p
for i = 1:m
namePlans(i) = textscan(nameFiles{1,i+2},'%1c', 1);
end
Metro.allNames = allNames;
Metro.dataCartesian=dataCartesian;
Metro.DataName=namePlans;
Metro.dataPol = dataPol;
Metro.PolDataCoef = PolDataCoef;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
%% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
%% function pushbutton1_Callback(hObject, eventdata, handles)
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
IndicePlan=get(handles.Choix_Plan,'Value');
if get(handles.radiobutton9,'Value')==1
flag_Couronne=true;
flag_Stack=false;
flag_Couronne_rayon=true;
flag_Couronne_periph=false;
elseif get(handles.radiobutton10,'Value')==1
flag_Couronne=true;
flag_Stack=false;
flag_Couronne_rayon=false;
flag_Couronne_periph=true;
else
flag_Couronne=false;
flag_Stack=false;
flag_Couronne_rayon = false;
flag_Couronne_periph = false;
end
if (flag_Couronne== false && flag_Stack == false)
disp('aucune fonction de selectionné 2')
return
elseif (flag_Couronne_rayon== false && flag_Couronne_periph == false)
disp('aucune fonction de selectionné 1')
return
elseif flag_Couronne_periph & isempty(get(handles.edit1,'String'))
disp('la fonction couronne necessite un facteur')
else
if flag_Couronne_rayon
Couronne_Func(IndicePlan,handles)
elseif flag_Couronne_periph
FacteurZoom=str2double(get(handles.edit1,'String'));
Couronne_periph_Func(FacteurZoom,IndicePlan,handles)
elseif flag_Stack
Stack_Func(handles,IndicePlan)
end
end
%% function Couronne_Func(FacteurZoom,handles)
function Couronne_periph_Func(FacteurZoom,IndicePlan,handles)
AllNames = handles.MetroAllNames;
for i = 3 : length(AllNames)
[~,pos] = textscan(AllNames{i},'%5c', 1);
nomsPhases2D{i-2} = AllNames{i}(pos+1:end);
end
CartesianData = handles.MetroData(:,IndicePlan);
CartDataCoef = CouronneCoef(handles,FacteurZoom,IndicePlan);
color_map = colormap(lines(length(CartesianData)));
axes(handles.Plot_final);
cla
for i = 1:length(CartesianData)
if i<length(CartesianData)
if FacteurZoom ~= 1
CartesianPlot = CartDataCoef{(i)};
plot(CartesianPlot(:,1),CartesianPlot(:,2),'color',color_map(i,:))
clear CartesianPlot
hold on
else
CartesianPlot = CartesianData{(i)};
plot(CartesianPlot(:,1),CartesianPlot(:,2),'color',color_map(i,:))
clear CartesianPlot
hold on
end
else
if FacteurZoom ~= 1
CartesianPlot = CartDataCoef{(i)};
plot(CartesianPlot(:,1),CartesianPlot(:,2),'color',color_map(i,:))
clear CartesianPlot
legend(nomsPhases2D)
else
CartesianPlot = CartesianData{(i)};
plot(CartesianPlot(:,1),CartesianPlot(:,2),'color',color_map(i,:))
clear CartesianPlot
legend(nomsPhases2D)
end
end
end
axis equal
grid
hold off
% disp('mettre l''inteligence de la fonction')
%% function CouronneCoef(handles,FacteurZoom,IndicePlan)
function DataCoef = CouronneCoef(handles,FacteurZoom,IndicePlan)
dataCartesian = handles.dataCartesian(:,IndicePlan);
[m,n] = size(dataCartesian);
for i = 1:m
for ii = 1:n
tempData = dataCartesian{i,ii};
tempX = tempData(:,1);
tempY = tempData(:,2);
[theta,rho] = cart2compass(tempX, tempY);
MinRho = min(rho);
rhoCoef = ((((rho-MinRho)).*FacteurZoom)+MinRho);
[tempX, tempY]= compass2cart(theta,rhoCoef);
DataCoef{i,ii} = [tempX, tempY];
end
end
%% function Couronne_Func(FacteurZoom,handles)
function Couronne_Func(IndicePlan,handles)
% disp('funcion couronne en cours')
AllNames = handles.MetroAllNames;
for i = 3 : length(AllNames)
[~,pos] = textscan(AllNames{i},'%5c', 1);
nomsPhases2D{i-2} = AllNames{i}(pos+1:end);
end
dataPol = handles.MetroDataPol;
tempN = size(dataPol);
tempDataPol = {dataPol{1:tempN(1),IndicePlan}};
[deltaPol,deltaMeanPol] = delta(tempDataPol);
axes(handles.Plot_final);
cla
plot(0:360,deltaPol)
hold on
plot(0:360,deltaMeanPol)
grid
legend(nomsPhases2D)
xlabel('Angle (°)')
ylabel('Delta rayon (mm)')
set(gca,'XTick',[0 180 360])
set(gca,'XTickLabel','0|180|360')
%% function deltaPol=delta(vecteur)
function [deltaPol,deltaMeanPol]=delta(vecteur) % vecteur est tjrs en cell
%% Traitement des données
%-------------------------------------------------------------------------%
% Preparation des vecteurs
pas_angle =1;%1;
angles = zeros(361/pas_angle,length(vecteur));
coordonees = zeros(361/pas_angle,length(vecteur));
deltaPol = zeros(361/pas_angle+pas_angle-1,length(vecteur));
deltaMeanPol = zeros(361/pas_angle+pas_angle-1,length(vecteur));
clear p
%-------------------------------------------------------------------------%
% Calcul des distances
%%
for p=1:length(vecteur);%:length(vecteur);
A = vecteur{p};
r = A(:,2);
r = r-3; % 6 = diamètre de bille du bras romer
theta = A(:,1);
clear pp
for pp = 0:360/pas_angle
[~,idx]=min(abs(theta-pp));
find(r==theta(idx));
angles(pp+1,(p)) = r(idx);
[u,v] = compass2cart(pp,r(idx));
coordonees(pp+1,(p)+(p)-1) = u;
coordonees(pp+1,(p)+(p)) = v;
end
clear idx
end
clear pp
for pp = 2:length(vecteur)
deltaPol(:,pp) = angles(:,pp)-angles(:,1);
deltaPol(:,pp) = medfilt1(deltaPol(:,pp),15); % medfilt1 :
deltaMeanPol(:,pp) = mean(deltaPol(:,pp));
end
%
%%save('dataPol')
%save('deltaPol')
%%
function Stack_Func(handles,IndicePlan)
% axes(handles.Plot_final);
% cla
% disp('mettre l''inteligence de la fonction')
% --- Executes during object creation, after setting all properties.
function radiobutton9_CreateFcn(hObject, eventdata, handles)
% hObject handle to radiobutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes when selected object is changed in uipanel6.
function uipanel6_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel6
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if strcmp(get(eventdata.NewValue,'string'),'périphérie')
set(handles.uipanelOption,'Visible','on')
else
set(handles.uipanelOption,'Visible','off')
end
% disp ('tu est dans la fonction périphérie')
% --------------------------------------------------------------------
function Zoom_Callback(hObject, eventdata, handles)
% hObject handle to Zoom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function ZoomIn_Callback(hObject, eventdata, handles)
% hObject handle to ZoomIn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function ZoomOut_Callback(hObject, eventdata, handles)
% hObject handle to ZoomOut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function AutoFit_Callback(hObject, eventdata, handles)
% hObject handle to AutoFit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
  1 Comment
Jan
Jan on 11 Aug 2022
Concentrate on posting the relevant part of the code only. This is less disturbing for the readers.

Sign in to comment.

Answers (1)

Jan
Jan on 11 Aug 2022
Do not append folders to Matlab path only to import files. This is a source of unexpected behavior. Include only the folders containing Matlab functions to the path. Neither addpath nor path(path, ...) is useful.
For data files use absolute pathnames instead:
% Nope: p = path;
% Nope: path(p,allNames{i})
newFiles = dir(allNames{i});
nameFiles = {newFiles.name};
for ii = 3:length(newFiles)
tempCart = importdata(fullfile(allNAmes{i}, nameFiles{ii})
Remember, that it is not documented, that "." and ".." are the first two elements of the reply of dir(). Better use strcmp to remove these special folders.
The error message means, that the file is not found. Try to use full path names to reduce the confusion level.
  2 Comments
Jan
Jan on 13 Aug 2022
Moved from secrions for answers:
Hello,
Thank you very much but I can't remove "." and ".." of my files...
I try a lot of thing but it is not working...
How can I remove it ?
Sorry, I am a beginnner in Matlab...
Jan
Jan on 13 Aug 2022
newFiles = dir(allNames{i});
nameFiles = {newFiles.name};
nameFiles(strcmp(nameFiles, '.') || strcmp(nameFiles, '..')) = [];
% Or:
% nameFiles = setdiff(nameFiles, {'.', '..'});
% Or:
% nameFiles(strncmp(nameFiles, '.', 1)) = []; % Removes all files starting
% % with a dot.
% Or in modern Matlab versions:
% nameFiles(startsWith(nameFiles, '.')) = [];
for ii = 1:length(nameFiles)
tempCart = importdata(fullfile(allNames{i}, nameFiles{ii})
Maybe "folderList" is a better name than "allNames".
Question of beginners are very welcome in this forum.

Sign in to comment.

Categories

Find more on Chemistry 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!