Specify What to Edit for User
Mostrar comentarios más antiguos
I'm writing code which should allow the user to edit certain sections, and I want to make it clear to the user where they should edit (it doesn't have to prevent them from editing other stuff). The user would select what they want to modify in the code (through a GUI) and it would take them to the correct section of the code. I thought of a number of options (unfolding only that section, disabling editing other code, moving to the correct line) but I couldn't find a command to do any of these things. (Having it display something in line through live editor won't work because it's a class). Does anyone know how you might implement this?
26 comentarios
Guillaume
el 10 de Jul. de 2019
Replicating a code editor in a matlab GUI would be a significant undertaking and I'm not even sure that it would be possible with the controls that matlab currently offer.
If I were going to build the kind of GUI you describe, I certainly wouldn't do it in matlab. I'd use a language where you can use already made code editor controls such as scintilla
On the other hand, forcing users to edit code doesn't sound like a good idea.
Ephraim Bryski
el 10 de Jul. de 2019
Editada: Ephraim Bryski
el 10 de Jul. de 2019
Guillaume
el 10 de Jul. de 2019
You already have a GUI that allows users to edit code?
Allowing users to extend a class by editing the class directly is very much against OOP principles. You normally use inheritance to extend classes.
Ephraim Bryski
el 10 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
Guillaume
el 12 de Jul. de 2019
I'm still not completely clear on exactly what help you're asking for. You said you already had the bulk of it done. The bulk of the GUI? As I said, I don't think it's possible to make a proper code editing GUI in matlab, the text control it offers is too basic.
In any case, if I was to make a GUI for user to specify some properties and call functions on them, I wouldn't ask them to do that in code. I'd use a UItable or similar so that the only task they have to ensure is that the property names and values are correct and that they've selected the correct function, rather than also having to ensure that the syntax is correct and that the code they've written stay within the boundary of what's allowed by your architecture.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: per isakson
el 30 de Jul. de 2019
Guillaume
el 12 de Jul. de 2019
Ok, reading about your description in that other post, I think your GUI should simply be matlab. And instead of making your own GUI, you should provide good documentation of the API.
Otherwise, I fear you'll be trying to replicate most of matlab editing facilities within a matlab GUI, which as I said, is not really feasible.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
@Ephraim Bryski: you seem to be putting a lot of effort into essentially replicating MATLAB functionality: e.g. adding functions to the Search Path, function name hints, listing functions,...
"...they could just call the function but then they would have to get the name just right"
At some point you have to trust your users. Providing a walled-compound for your users, within which every little operation is carefully micro-managed and restricted, might possibly be achievable, but the code will be bloated, fragile, and difficult to maintain. And user-unfriendly.
As Guillaume wrote, providing good documentation (and input testing) is more useful than forcing your users into interacting with your code in one particular way. If you want the users to be able to add functions, then give them that responsibility: let them supply function handles as input arguments, or they can change the search path by hand, or whatever. Document it clearly, and throw an error if they do not do it properly.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
"As for API, I don't know what to say about that because I honestly had to search it up to find out what it means"
Application programming interface. You've defined a set of classes which must adhere to some structure, and you've defined a set of functions that must be called with some particular input and will return some particular output. That's the API of your code. By documenting it, you're telling your user, if you call this function with these arguments, you'll get this result. If your class has these particular properties, then it will work with my functions, etc...
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
"Ok for docoment I imagine you mean something detects what the search path is..."
Not at all. I mean that you should write clear documentation that specifies exactly what inputs and outputs your code has, what other specific requirements it has, explains how they can expect it to behave, and gives some examples of its usage.
I would not "detect what the search path is" unless I was writing a "detect the Search Path" tool.
Ephraim Bryski
el 12 de Jul. de 2019
Guillaume
el 12 de Jul. de 2019
Would this still be an API?
The API is basically a contract between your code and the user. You tell the user: to achieve this task, you use this function/class/whatever with these inputs, and you get that in return. As long as they follow the contract, everything works (baring bugs), if they don't stick to their side of the contract (supplying required inputs that follow the contract), then they're outside of the API and shouldn't have any expectation that anything work (it may or may not). The API can be made of many functions/classes or just one.
You don't have to call it an API. What is important as Stephen and I said, is that you document your interface. See the matlab documentation for an example of fairly good documentation. An important part of a documentation is explaining what your function expects as input and how that affects the output. Writing good documentation requires some effort. However, undocumented code is useless to anybody but the author (and even then, 6 months later the author may have forgotten).
Note that documenting your API includes specifying what class of inputs are expected (numeric, string, char array, cell array, logical, etc.), what shape is allowed (scalar, row vector, column vector, matrix, ND array, etc.). Same for outputs. Using validateattributes at the beginning of a function is a good way to check that the user is conforming to your contract.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
Stephen23
el 12 de Jul. de 2019
"...include errors within the code if the user doesn't do something right"
Actually disp is not so far from the answer: use error and assert and warning as required. Check that inputs meet your specifications using your own checks or an input parser.
"Also, how would I go about directing them?"
Throw an error if something happens which your code should not handle, with a clear error message explaining the cause of the error. You do not need to "direct" them to read your documentation, which they should be doing anyway.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
"Where will they read it (or essentially where will I write it)?"
The simplest is simply by adding a help section at the start of functions (which can then automatically be shown in a browser/dialog similarly to MATLAB's own help):
or in HTML/whatever documentation created using publish:
or using much more sophisticated toolbox documentation:
or by providing demo scripts, or whatever suits your workflow.
"...is there a way of changing the variable name of an object through all my code (instead of just a single function)"
Not really.
But the variable name really should not matter anyway: variable names used inside any function should be totally irrelevant to the outside world. There is no reason why the name of some value should be the same in every different function workspace, just pick names which are suitable and relevant for each function.
Ephraim Bryski
el 12 de Jul. de 2019
Guillaume
el 12 de Jul. de 2019
At the most basic, documentation can be just comments at the start of the function or within the class. Later on, you can do something more fancy (follow the links at the bottom of that documentation), or write your documentation as a Word/PDF/whatever takes your fancy.
Here is an example of class documentation that I'd consider good. You can see that the DrawText method relies on an inputParser to check that the user conformed to the documented API.
classdef (Sealed) ImageCanvas < handle
%ImageCanvas: Allows drawing text and shapes onto images
%All the drawing is delegate to .Net, so this only works on Windows.
%
%In order to draw on an image, it must first be converted into a canvas.
%All drawing is done onto the canvas, and when needed, the canvas can then be converted back into an image
%Note that canvas are always 8-bit true colour images. Original images in other format will always be converted to that format
%A canvas is created simply by passing an input image to the ImageCanvas constructor.
%
% canvas = ImageCanvas(someimage);
%
%Member methods of ImageCanvas can then be called to draw on the canvas.
%All drawing methods will require at least one of the following object as well(unless defaults are used):
% Font: for text drawing methods, specify which font and font style to use.
% Brush: for any drawing with filled shapes (including text), specify colour and texture of the filled shape.
% Brush has several derived implementations:
% SolidBrush: A basic brush that fills the shape with a solid colour.
% HatchBrush: A brush that fills the shape with a hatch pattern.
% Pen: for drawing anything with lines, specify the colour, thickness, endpoints, etc. of the lines
%
%To retrieve an image, after having drawn on the canvas, call GetImage:
%
% newimage = canvas.GetImage;
%
%The width and height of the canvas can be queried at any time, it's the same width and height as the input image:
%
% width = canvas.Width;
% height = canvas.Height;
%
%The following methods are currently implemented:
% DrawText(Text, Location, name, value...): Draw text at specific location.
% Text The text to draw on the image. char row vector or scalar string.
% Location Location in pixels of the upper left corner of the text box as a 2 element vector [x, y] of any numeric type. Coordinates are .Net based. Upper left corner of the image is [0,0]
% The following name/value parameters are available
% 'Font', font Use the given font to draw the text, can be a Font object or a .Net System.Drawing.Font object. If unspecified, matlab's default font is used.
% 'Brush', brush Use the given brush to draw the text, can be any Brush derived object or a .Net System.Drawing.Brush object. If unspecified, a solid black brush is used.
% 'BoxSize', [width, height] Clips text to box size in pixels. If not specified the text is clipped to the image.
%
[...] code removed
properties (Dependent)
Width; %Width of the canvas. Same as the original image.
Height; %Height of the canvas. Same as the original image.
end
methods
function this = ImageCanvas(image)
%Construct an ImageCanvas from an image
%the image is stored as a truecolour 8-bit image
% canvas = ImageCanvas(image)
% image: 2D or 3D numeric matrix. if double, intensities must be in the range 0-1. Indexed images are not supported
[...] code removed
end
function DrawText(this, varargin)
%Draw text onto the canvas
% canvas.DrawText(Text, Location, name, value...)
% Text The text to draw on the image. char row vector or scalar string.
% Location Location in pixels of the upper left corner of the text box as a 2 element vector [x, y] of any numeric type. Coordinates are .Net based. Upper left corner of the image is [0,0]
% The following name/value parameters are available
% 'Font', font Use the given font to draw the text, can be a Font object or a .Net System.Drawing.Font object. If unspecified, matlab's default font is used.
% 'Brush', brush Use the given brush to draw the text, can be any Brush derived object or a .Net System.Drawing.Brush object. If unspecified, a solid black brush is used.
% 'BoxSize', [width, height] Clips text to box size in pixels. If not specified the text is clipped to the image.
%
parser = inputParser;
parser.FunctionName = 'ImageCanvas.DrawText';
parser.addRequired('Text', @(text) validateattributes(text, {'char', 'string'}, {'scalartext'}));
parser.addRequired('Location', @(location) validateattributes(location, {'numeric'}, {'numel', 2, 'nonnegative', 'finite'}));
parser.addOptional('Font', Font, @(font) validateattributes(font, {'Font', 'System.Drawing.Font'}, {'scalar'}));
parser.addOptional('Brush', SolidBrush, @(brush) validateattributes(brush, {'Brush', 'System.Drawing.Brush'}, {'scalar'}));
parser.addOptional('BoxSize', [], @(boxsize) validateattributes(boxsize, {'numeric'}, {'numel', 2, 'nonnegative', 'finite', 'real'}));
parser.parse(varargin{:});
[...] code removed
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
"...won't you still need the variable name of the object?"
I think I misunderstood your original question due to the terminology: objects have methods, properties, and events, but they do not have variables:
I was using the standard MATLAB terminology, where a 'variable' refers to an instance of some class (I have never used any OOP language where the term 'variable' refers to a class attribute).
I suspect that you wanted to ask "... won't you still need the object's property/method name?" , in which case the answer is "yes". If you meant something else, please explain.
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
Ephraim Bryski
el 12 de Jul. de 2019
Editada: Ephraim Bryski
el 12 de Jul. de 2019
Ephraim Bryski
el 13 de Jul. de 2019
Respuestas (1)
Image Analyst
el 12 de Jul. de 2019
0 votos
I'd just use app designer or guide and make a gui with either 3 radio buttons or a drop down list (popup) with three selections. Then in the main part of your code, you can check the state of the GUI controls to see which of your available options they selected.
4 comentarios
Image Analyst
el 12 de Jul. de 2019
If you need an example, see MAGIC: https://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component
Ephraim Bryski
el 12 de Jul. de 2019
Image Analyst
el 13 de Jul. de 2019
I still think you're making it way more complicated than necessary by doing such low level API coding manually.
Ephraim Bryski
el 13 de Jul. de 2019
Categorías
Más información sobre Performance and Memory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!