Skip to content
Snippets Groups Projects
Commit 863a6937 authored by Nicola Vigano's avatar Nicola Vigano
Browse files

gtModifyStructure: fixed a bug in visualization of doubles and reformatted

parent 27ed499c
No related branches found
No related tags found
No related merge requests found
function Structure = gtModifyStructure(Structure, varargin)
function Structure = gtModifyStructure(Structure, varargin)
% GTMODIFYSTRUCTURE GUI to modify a Structure - eg parameters.mat
% Structure = gtModifyStructure(Structure, varargin)
% Structure = gtModifyStructure(Structure, varargin)
% --------------------------------------------------
%
% Version 003 16/08/2012 by AKing
......@@ -15,9 +15,9 @@ function Structure = gtModifyStructure(Structure, varargin)
%
% to reduce length of display, can pass in a list of which fields to
% modify. Other data is kept, but not displayed. The list of field names is a cell
% array of strings: list{1,1}='nproj', list{2,1}='name', etc.
% array of strings: list{1, 1} = 'nproj', list{2, 1} = 'name', etc.
% if you want captions to help the users, these are a second column to the
% call array: list{1,2}='how many projections?', list{2,2}='name of
% call array: list{1, 2} = 'how many projections?', list{2, 2} = 'name of
% dataset?'
%
% You can also pass in a header string as a third argument, eg:
......@@ -33,20 +33,19 @@ function Structure = gtModifyStructure(Structure, varargin)
%
% could add the datatype to the display
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% are we doing only certain fields of the structure?
% if so, unpack the list and captions (if supplied)
list=[];
captions=[];
if length(varargin)>=1
tmp=varargin{1}; % cell array
if size(tmp, 2)==1 % no captions
list=tmp;
elseif size(tmp, 2)==2 % unpackage
for i=1:size(tmp, 1)
list{i}=tmp{i,1};
captions{i}=tmp{i,2};
list = [];
captions = [];
if length(varargin) >= 1
tmp = varargin{1}; % cell array
if size(tmp, 2) == 1 % no captions
list = tmp;
elseif size(tmp, 2) == 2 % unpackage
for i = 1:size(tmp, 1)
list{i} = tmp{i, 1};
captions{i} = tmp{i, 2};
end
elseif isempty(tmp)
% no list or captions passed in
......@@ -54,99 +53,93 @@ if length(varargin)>=1
disp('did not understand the "list" argument')
end
end
headertext=[];
if length(varargin)==2
headertext=varargin{2};
headertext = [];
if length(varargin) == 2
headertext = varargin{2};
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get started
% convert to cell Structure of strings
CellStructure=sfStruct2Cell(Structure, list, captions);
CellStructure = sfStruct2Cell(Structure, list, captions);
% required
quit=0;
quit = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% faff around setting the sizes of things for the GUI
dim.ScreenSize=get(0, 'ScreenSize');
dim.ScreenSize = get(0, 'ScreenSize');
% now open the figure
% how big do we need?
% horizontal size
char_spacing=8;
namelength=1;
vallength=1;
captionlength=1;
objectspacing=10;
char_spacing = 8;
namelength = 1;
vallength = 1;
captionlength = 1;
objectspacing = 10;
% find the longest fields
for i=1:length(CellStructure)
namelength=max(namelength, length(CellStructure(i).name));
vallength=max(vallength, length(CellStructure(i).val));
captionlength=max(captionlength, length(CellStructure(i).caption));
for i = 1:length(CellStructure)
namelength = max(namelength, length(CellStructure(i).name));
vallength = max(vallength, length(CellStructure(i).val));
captionlength = max(captionlength, length(CellStructure(i).caption));
end
% increase val length because sometimes variables get longer!
vallength=vallength*1.3;
vallength = vallength*1.3;
% impose some sensible maximum, regardless of screensize
namelength=min(40, namelength);
vallength=min(110, vallength);
captionlength=min(70, captionlength);
namelength = min(40, namelength);
vallength = min(110, vallength);
captionlength = min(70, captionlength);
% Also impose some sensible minium
namelength=max(15, namelength);
vallength=max(15, vallength);
captionlength=max(15, captionlength);
namelength = max(15, namelength);
vallength = max(15, vallength);
captionlength = max(15, captionlength);
% what figure width would this imply?
dim.fwidth=(namelength+vallength+captionlength)*char_spacing+(4*objectspacing);
dim.fwidth = (namelength+vallength+captionlength)*char_spacing+(4*objectspacing);
% does this fit on the screen?
if dim.fwidth<(dim.ScreenSize(3)*0.9);
dim.lwidth=namelength*char_spacing;
dim.bwidth=vallength*char_spacing;
dim.cwidth=captionlength*char_spacing;
dim.lwidth = namelength*char_spacing;
dim.bwidth = vallength*char_spacing;
dim.cwidth = captionlength*char_spacing;
else
dim.fwidth=dim.ScreenSize(3)*0.9;
dim.lwidth=namelength*char_spacing;
dim.bwidth=vallength*char_spacing;
dim.fwidth = dim.ScreenSize(3)*0.9;
dim.lwidth = namelength*char_spacing;
dim.bwidth = vallength*char_spacing;
% crop the caption
dim.cwidth=(dim.fwidth-dim.lwidth-dim.bwidth-40);
dim.cwidth = (dim.fwidth-dim.lwidth-dim.bwidth-40);
end
% do we have some header text?
if ~isempty(headertext)
dim.headertext=headertext;
dim.headerspace=30; % one row
dim.headertext = headertext;
dim.headerspace = 30; % one row
else
dim.headertext=[];
dim.headerspace=0;
dim.headertext = [];
dim.headerspace = 0;
end
% objects are 20 pixels high in 30 pixel space
dim.vspacing=30;
dim.bheight=20;
dim.vspacing = 30;
dim.bheight = 20;
% how many rows can we fit in? 30 pixel for each line, plus space for button
maxfields=floor((dim.ScreenSize(4)-300-dim.headerspace)/dim.vspacing);
maxfields = floor((dim.ScreenSize(4)-300-dim.headerspace)/dim.vspacing);
% if we don't have space, split into several sequential figures
if maxfields<length(CellStructure)
firsts=1:maxfields:length(CellStructure);
lasts=firsts+maxfields-1;
lasts(end)=length(CellStructure);
if maxfields < length(CellStructure)
firsts = 1:maxfields:length(CellStructure);
lasts = firsts+maxfields-1;
lasts(end) = length(CellStructure);
else
firsts=1;
lasts=length(CellStructure);
firsts = 1;
lasts = length(CellStructure);
end
for j=1:length(firsts)
for jj = 1:length(firsts)
[fh, CellStructure]=sfBuildFigure(CellStructure, firsts(j), lasts(j), Structure, dim);
[fh, CellStructure] = sfBuildFigure(CellStructure, firsts(jj), lasts(jj), Structure, dim);
% wait until finished - thanks Greg
while ~quit
......@@ -155,7 +148,7 @@ for j=1:length(firsts)
% quit function
close(fh)
quit=0;
quit = 0;
end
return
......@@ -163,18 +156,17 @@ return
end % end function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subfunction functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [fh, CellStructure]=sfBuildFigure(CellStructure, first, last, Structure, dim)
function [fh, CellStructure] = sfBuildFigure(CellStructure, first, last, Structure, dim)
% first, what is the height of this figure?
dim.fheight=dim.vspacing*(last-first+1)+80+dim.headerspace;
dim.fheight = dim.vspacing*(last-first+1)+80+dim.headerspace;
% now can create the figure
fh=figure('Visible', 'off', 'Position', [(dim.ScreenSize(3)-dim.fwidth)/2 (dim.ScreenSize(4)-dim.fheight)/2 dim.fwidth dim.fheight],...
fh = figure('Visible', 'off', 'Position', ...
[(dim.ScreenSize(3)-dim.fwidth)/2 (dim.ScreenSize(4)-dim.fheight)/2 dim.fwidth dim.fheight], ...
'menubar', 'none');
% building the GUI
......@@ -189,32 +181,39 @@ if ~isempty(dim.headertext)
end
% now add the lines of name / editable value / caption
for i=first:last
for ii = first:last
% position of objects
dim.bposx=10;
dim.bposy=dim.fheight-((i-first+1)*dim.vspacing)-30-dim.headerspace;
dim.bposx = 10;
dim.bposy = dim.fheight-((ii-first+1)*dim.vspacing)-30-dim.headerspace;
% add the objects
% field name
CellStructure(i).label=uicontrol(fh, 'Style', 'text', 'String', CellStructure(i).name, ...
CellStructure(ii).label = uicontrol(fh, 'Style', 'text', 'String', CellStructure(ii).name, ...
'Position', [dim.bposx, dim.bposy, dim.lwidth, dim.bheight]);
% editable string for the field value (or checkbox for boolean)
if CellStructure(i).type==2
CellStructure(i).object=uicontrol(fh, 'Style', 'checkbox', 'value', CellStructure(i).val,...
if (CellStructure(ii).type == 2)
% It is a boolean
CellStructure(ii).object = uicontrol(fh, 'Style', 'checkbox', 'value', CellStructure(ii).val, ...
'Position', [dim.bposx+dim.lwidth+10+(dim.bwidth/2), dim.bposy, dim.bheight, dim.bheight]);
else
CellStructure(i).object=uicontrol(fh, 'Style', 'edit', 'String', CellStructure(i).val, ...
if ((CellStructure(ii).type == 1) && ~ischar(CellStructure(ii).val))
% It is a double and is not converted, yet.
val = num2str(CellStructure(ii).val);
else
% It is either a string or a double in string form
val = CellStructure(ii).val;
end
CellStructure(ii).object = uicontrol(fh, 'Style', 'edit', 'String', val, ...
'Position', [dim.bposx+dim.lwidth+10, dim.bposy, dim.bwidth, dim.bheight]);
end
% add the helpful caption
CellStructure(i).captionh=uicontrol(fh, 'Style', 'text', 'String', CellStructure(i).caption, ...
CellStructure(ii).captionh = uicontrol(fh, 'Style', 'text', 'String', CellStructure(ii).caption, ...
'Position', [dim.bposx+dim.lwidth+10+dim.bwidth+10, dim.bposy, dim.cwidth, dim.bheight]);
end
% add OK button at bottom of figure
savebutton=uicontrol(fh, 'Style', 'pushbutton', 'String', 'OK', ...
'Position', [(dim.fwidth/2)-40 dim.fheight-((last-first+1)*30)-70-dim.headerspace 80 30],...
savebutton = uicontrol(fh, 'Style', 'pushbutton', 'String', 'OK', ...
'Position', [(dim.fwidth/2)-40 dim.fheight-((last-first+1)*30)-70-dim.headerspace 80 30], ...
'callback', {@sfCell2Struct, Structure, CellStructure, first, last});
% show GUI
......@@ -223,41 +222,41 @@ set(fh, 'Visible', 'on')
end % end function sfBuildFigure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Structure=sfCell2Struct(hObject, eventdata, Structure, CellStructure, first, last)
function Structure = sfCell2Struct(hObject, eventdata, Structure, CellStructure, first, last)
% do the reverse of sfStructure2Cell - read the cell Structure back into the
% Structure variable, making sure teh data types are right.
for i=first:last
if CellStructure(i).type==1 % numeric fields
for ii = first:last
if CellStructure(ii).type == 1 % numeric fields
% read out the updated value
CellStructure(i).val=get(CellStructure(i).object, 'String');
CellStructure(ii).val = get(CellStructure(ii).object, 'String');
% is this still a number? - if not it may have turned into a string
if isempty(str2num(CellStructure(i).val))
Structure.(CellStructure(i).name)=CellStructure(i).val;
if isempty(str2num(CellStructure(ii).val))
Structure.(CellStructure(ii).name) = CellStructure(ii).val;
else
if CellStructure(i).transposed
Structure.(CellStructure(i).name)=str2num(CellStructure(i).val)';
if CellStructure(ii).transposed
Structure.(CellStructure(ii).name) = str2num(CellStructure(ii).val)';
else
Structure.(CellStructure(i).name)=str2num(CellStructure(i).val);
Structure.(CellStructure(ii).name) = str2num(CellStructure(ii).val);
end
end
elseif CellStructure(i).type==2 % boolean fields
CellStructure(i).val=get(CellStructure(i).object, 'value');
Structure.(CellStructure(i).name)=CellStructure(i).val;
elseif CellStructure(i).type==3 % string fields
CellStructure(i).val=get(CellStructure(i).object, 'String');
% Is this still a string? (avoid warning message about matlab margin function)
if strcmp(CellStructure(i).val, 'margin') || (length(CellStructure(i).val)>=3 && strcmp(CellStructure(i).val(1:3), '00:')) || isempty(str2num(CellStructure(i).val))
Structure.(CellStructure(i).name)=CellStructure(i).val;
elseif CellStructure(ii).type == 2 % boolean fields
CellStructure(ii).val = get(CellStructure(ii).object, 'value');
Structure.(CellStructure(ii).name) = CellStructure(ii).val;
elseif CellStructure(ii).type == 3 % string fields
CellStructure(ii).val = get(CellStructure(ii).object, 'String');
% Is this still a string? (avoid warning message about matlab margin/inline function)
if (ismember(CellStructure(ii).val, {'margin', 'inline'}) ...
|| (length(CellStructure(ii).val) >= 3 && strcmp(CellStructure(ii).val(1:3), '00:')) ...
|| isempty(str2num(CellStructure(ii).val)) )
Structure.(CellStructure(ii).name) = CellStructure(ii).val;
else % if not, it is now a number
Structure.(CellStructure(i).name)=str2num(CellStructure(i).val);
Structure.(CellStructure(ii).name) = str2num(CellStructure(ii).val);
end
end
end
% have to pass this back to the main workspace (?)
quit=1;
quit = 1;
assignin('caller', 'Structure', Structure)
assignin('caller', 'quit', quit)
......@@ -266,7 +265,7 @@ end % end function sfCell2Struct
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function CellStructure=sfStruct2Cell(Structure, list, captions)
function CellStructure = sfStruct2Cell(Structure, list, captions)
% turn a Structureure into a cell array of strings for easier handling
% read out the values, convert to strings, and record what they were to
% start with
......@@ -274,58 +273,58 @@ function CellStructure=sfStruct2Cell(Structure, list, captions)
% remove fields which are themselves structures
% if list, do only those fields mentioned in list - reduce the size of the
% thingy. Not case sensitive, but preserves fieldnames
fnames=fieldnames(Structure);
count=0;
for i=1:length(fnames)
fnames = fieldnames(Structure);
count = 0;
for i = 1:length(fnames)
if isempty(list) || any(strcmpi(list, fnames{i}))
% get the field value
val=Structure.(fnames{i});
val = Structure.(fnames{i});
% skip this if it's a structure, or if it's a matrix
if isstruct(val) || (length(size(val))>1 && (size(val,1)>1 && size(val, 2)>1))
if isstruct(val) || (length(size(val))>1 && (size(val, 1)>1 && size(val, 2)>1))
continue
end
% otherwise, count item
count=count+1;
count = count+1;
% field name
CellStructure(count).name=fnames{i};
CellStructure(count).transposed=false; % default
CellStructure(count).name = fnames{i};
CellStructure(count).transposed = false; % default
% get caption if supplied
% which item from list?
listndx=find(strcmpi(list, fnames{i}));
listndx = find(strcmpi(list, fnames{i}));
if listndx && ~isempty(captions)
CellStructure(count).caption=captions{listndx};
CellStructure(count).caption = captions{listndx};
else
CellStructure(count).caption='';
CellStructure(count).caption = '';
end
% Determine field type, treat appropriately
if isnumeric(val) % numeric fields
if length(val)>1
if size(val, 1)>1
val=val';
CellStructure(count).transposed=true;
if length(val) > 1
if size(val, 1) > 1
val = val';
CellStructure(count).transposed = true;
end
val=num2str(val);
val = num2str(val);
% remove excess spaces
j=1;
while j<(length(val)-2)
if val(j)==' ' && val(j+1)==' ' && val(j+2)==' '
val(j)=[];
j = 1;
while j < (length(val)-2)
if val(j) == ' ' && val(j+1) == ' ' && val(j+2) == ' '
val(j) = [];
else
j=j+1;
j = j+1;
end
end
else
val=num2str(val);
val = num2str(val);
end
CellStructure(count).val=val;
CellStructure(count).type=1;
CellStructure(count).val = val;
CellStructure(count).type = 1;
elseif islogical(val) % boolean fields - can keep these as numbers
CellStructure(count).val=val;
CellStructure(count).type=2;
CellStructure(count).val = val;
CellStructure(count).type = 2;
else % string fields
CellStructure(count).val=val;
CellStructure(count).type=3;
CellStructure(count).val = val;
CellStructure(count).type = 3;
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment