Looping through GUI imagesc, but greyscale and reverse?

10 Ansichten (letzte 30 Tage)
Simpson
Simpson am 19 Jun. 2017
Kommentiert: Walter Roberson am 20 Jun. 2017
I currently have a code that looped through multiple different netcdf files to display (for example, 14 different images). The following is my code within the "opening function" function within the GUI:
% Choose default command line output for test
handles.output = hObject;
handles.files = dir('C:\Users\Micheal\Documents\MATLAB\Test2\*.netcdf');
for i = 1:length(handles.files)
filenames = [handles.files(i,1).name];
ncid = netcdf.open(filenames,'NC_NOWRITE');
[varname, xtype, varDimIDs, varAtts] = netcdf.inqVar(ncid,0);
Then a whole bunch of calculations are presented which are not included here
% handles.X{i} = Zfilenames(i,1).Z;
netcdf.close(ncid)
imagesc(handles.X{i}');
colormap('jet')
title(filenames)
end
handles.index = 1;
Cek(hObject,eventdata,handles)
% Update handles structure
guidata(hObject, handles);
I then have my two buttons, back and next which will allow me to loop through the images
% % --- Executes on button press in Back.
function Back_Callback(hObject, eventdata, handles)
% hObject handle to Back (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.output = hObject;
handles.index = handles.index - 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
% --- Executes on button press in Next.
function Next_Callback(hObject, eventdata, handles)
% hObject handle to Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.output = hObject;
handles.index = handles.index + 1;
Cek(hObject, eventdata, handles);
imshow(handles.X{handles.index},[]);
guidata(hObject, handles);
function Cek(hObject, eventdata, handles)
handles.output = hObject;
n = length(handles.files);
if handles.index > 1, set(handles.Back,'enable','on');
else set(handles.Back,'enable','off'); end
if handles.index < n, set(handles.Next,'enable','on');
else set(handles.Next,'enable','off'); end
guidata(hObject, handles);
What happens is that my GUI presents the last image within my list of filenames with the correct orientation, colormap, and title. However, when I select "Next", everything becomes greyscale, with the incorrect orientation, and no title. Where am I missing the updated axes and colormap and title options? Any help would be greatly appreciated.

Antworten (1)

Walter Roberson
Walter Roberson am 19 Jun. 2017
I do not know offhand about the orientation, but the title and grayscale problems are obvious.
You are using imshow() to draw each image. imshow() is a "higher level graphics function". Like all of the higher level graphics functions, it checks the state of "hold on", and if that is not set then it does a cla() before adding the image. The cla() removes the active title and colormap.
You should do the imshow() once right at the beginning, and record the handle of the image() object returned. After that, at each step forward or back you should set the image object CData to the new content. That will update the image without changing the title or colormap. Chances are that you will also be wanting to record the handle returned by title() and update its String property to reflect the image being viewed.
By the way, your code to read the images has a bug. You should change
handles.files = dir('C:\Users\Micheal\Documents\MATLAB\Test2\*.netcdf');
for i = 1:length(handles.files)
filenames = [handles.files(i,1).name];
to something more like
projectdir = 'C:\Users\Micheal\Documents\MATLAB\Test2'
handles.files = dir( fullfile(projectdir, '*.netcdf') );
for i = 1:length(handles.files)
filenames = fullfile(projectdir, handles.files(i,1).name );
Otherwise you are asking netcdf to read from files that might be in some other directory that it does not know about.
  4 Kommentare
Walter Roberson
Walter Roberson am 20 Jun. 2017
I, on the other hand, resist using imshow() as it does weird things like resizes the figure.
Walter Roberson
Walter Roberson am 20 Jun. 2017
% Choose default command line output for test
projectdir = 'C:\Users\Micheal\Documents\MATLAB\Test2';
files = dir( fullfile(projectdir, '*.netcdf') );
for i = 1 : length(files)
filenames{i} = fullfile( projectdir, files(i).name );
ncid = netcdf.open( filenames{i}, 'NC_NOWRITE');
[varname, xtype, varDimIDs, varAtts] = netcdf.inqVar(ncid, 0);
%Then a whole bunch of calculations are presented which are not included here
handles.X{i} = Zfilenames(i,1).Z;
netcdf.close(ncid)
%no point in displaying the intermediate images as only the last one will be left on the screen
end
handles.filenames = filenames;
handles.index = 1;
handles.OutImage = imagesc( handles.X{1}.' );
ax = ancestor(handles.OutImage, 'axes');
colormap(ax, 'jet')
[~, basename, ~] = fileparts( filenames{i} );
handles.OutTitle = title(ax, basename);
guidata(hObject, handles);
Cek(hObject,eventdata, handles)
% --- Executes on button press in Back.
function Back_Callback(hObject, eventdata, handles)
% hObject handle to Back (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.index = handles.index - 1;
set(handles.OutImage, 'CData', handles.X{handles.index}.' );
[~, basename, ~] = fileparts( handles.filenames{handles.index} );
set(handles.OutTitle, 'String', basename);
guidata(hObject, handles);
Cek(hObject, eventdata, handles);
% --- Executes on button press in Next.
function Next_Callback(hObject, eventdata, handles)
% hObject handle to Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.index = handles.index + 1;
set(handles.OutImage, 'CData', handles.X{handles.index}.' );
[~, basename, ~] = fileparts( handles.filenames{handles.index} );
set(handles.OutTitle, 'String', basename);
guidata(hObject, handles);
Cek(hObject, eventdata, handles);
function Cek(hObject, eventdata, handles)
states = {'off', 'on'};
n = length(handles.filenames);
set(handles.Back, 'enable', states{(handles.index > 1) + 1});
set(handles.Next, 'enable', states{(handles.index < n) + 1});

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by