314 views (last 30 days)
Show older comments
Ruben on 2 Dec 2014
Answered: Salvador Castaneda on 23 Oct 2021
Accepted Answer: dpb
Open in MATLAB Online
This question maybe a bit like the link below, but this didn't work for me... http://nl.mathworks.com/matlabcentral/answers/102530-how-can-i-change-the-default-settings-for-the-linewidth-property-before-i-plot-a-figure-in-matlab
I'm working on a matlab function that automatically opens your figure in full screen mode and on a second monitor if present. So far, everything works fine. I already achieved to set the fontsize inside the function, so whitout plotting anything and without making xlabel(..) etc.:
% Fontsize used at the figure
if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)
set(gca,'FontSize',16)
else
set(gca,'Fontsize',fontsize_manual)
end
Now is my question: Can I change in a same way the linewidth of the lines that will by plotted in the figure? So also here, predefining the linewidth inside the function and later on in your script plotting some lines etc. I do prefer this works only for the figure you're working on, so that you can change this 'default' for each figure and save them all with different linewidth and fontsizes if needed.
I tried the line below, but that only changed the linewidth of the axis.
set(gca,'LineWidth',2)
Is there anyone who can help me solving this problem?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
dpb on 2 Dec 2014
Open in MATLAB Online
Use the technique of the referenced page but set the default for the figure in question --
set(gca,'DefaultLineLineWidth',2)
NB: the default properties are named in run-on fashion--the name of the object you're setting then the property within that object all preceded with the keyword 'Default'. Here that's
Default:Line:LineWidth
to separate the subsets visually. I think it would've been easier if TMW had used such nomenclature or a structure form instead (and probably the parsing internally would've been simpler as well).
9 Comments Show 7 older commentsHide 7 older comments
Show 7 older commentsHide 7 older comments
Ruben on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253587
That was the only one I did not tried. This actually worked for the first figure and not for the second time I call the function in a matlab session. However, changing 'gca' into 'gcf' gave the required result for both figures! Thanx!
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253591
Open in MATLAB Online
But now I run into a new problem... The code below in found accidentally by solving the previous problem:
set(gca,'LineWidth',3)
It turned out this changes the width of the axes. But now the problem... Also here this works only on the first figure. (see figure)
If I also put this code in my session after the plotting in the second figure, the width in the second figure changes. Looks like the right handle isn't reached, or something like that, inside the function, when making the second figure. Do you have any idea what could be wrong here?
Ruben on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253611
Open in MATLAB Online
same holds for:
set(gca,'FontName','Arial')
dpb on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253617
Yes, you've got to be certain you're looking at the right object-- gca and gcf are what they say--the CURRENT axes or figure which may (or may not) actually be the one you're aiming to modify when your code makes the call.
When doing stuff like this you need to always save the handles of the objects when created and use those to ensure you modify the desired one.
How to solve the above problem specifically would depend on seeing the code that generated the figures in sequence and in context.
Ruben on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253624
Open in MATLAB Online
This will be (a part of) the plotting in the test-session...
figuur1=fancy_figure('open','figuur1',[],[],[],[]);
lijn1=plot(x(4:5),[epsilon(4) epsilon(4)],'k');%,'linewidth',2);
figuur2=fancy_figure('open','figuur2',[],[],[],5);
plot(x(4:5),[epsilon(4) epsilon(4)],'k')
And this will be the function that I seperately saved as fancy_figure.m
function [figure_handle]= fancy_figure(action,figure_handle,...
save_name,save_format,fontsize_manual,linewidth_manual)
windows 7 tested and matlab R2014a
%%Function code for plotting full screen figure and do this on second monitor if present.
% If the 'action' input argument equals 'save' the figure will be saved
% with the settings requested.
switch action
case 'open'
%%Open a figure on full screen and on the second monitor if present.
% Format of monitor_positions in pixels: [xMin yMin xMax yMax]
mon_pos = get(0,'MonitorPositions');
size_monitor = size(mon_pos);
if (size_monitor(1) > 1)
figure_handle = figure('Position', [mon_pos(2,1:2) 50 50]);
pause(0.1);
set(get(handle(gcf),'JavaFrame'),'Maximized',1);
else
figure_handle=figure;
pause(0.01);
set(get(handle(gcf),'JavaFrame'),'Maximized',1);
end
% Fontsize used at the figure
if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)
set(gca,'FontSize',16)
set(gca,'FontName','Dotum')
else
set(gca,'Fontsize',fontsize_manual)
set(gca,'FontName','Dotum')
end
dpb on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253690
Edited: dpb on 6 Dec 2014
Open in MATLAB Online
Well, your code is rife with what I said specifically to not do... :)
...
if (size_monitor(1) > 1)
figure_handle = figure('Position', [mon_pos(2,1:2) 50 50]);
pause(0.1);
set(get(handle(gcf),'JavaFrame'),'Maximized',1);
...
Here you just created a figure and save its handle but then turn around and use gcf instead of that specific handle. While this particular one is going to be ok since they are inline, it's poor practice.
On down just a little ways, the source of your problem is
% Fontsize used at the figure
if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)
set(gca,'FontSize',16)
set(gca,'FontName','Dotum')
...
where you have created a figure but there is no axes object on it as yet. Hence gca gets the last axis that was drawn upon, not the one that will (eventually, presumably) be created on that figure.
I repeat, you have to be sure you're addressing the object you want...
dpb on 4 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253732
Open in MATLAB Online
And, the calling sequence you show of
figuur1=fancy_figure('open','figuur1',[],[],[],[]);
lijn1=plot(x(4:5),[epsilon(4) epsilon(4)],'k');%,'linewidth',2);
figuur2=fancy_figure('open','figuur2',[],[],[],5);
...
causes the first figure to be opened, then an axes is created and plotted onto with plot prior to the subsequent call to fancy_figure. At that point, that is the current axis and the handle which gca will return...
Ruben on 4 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253943
That was indeed the kind of code badness I expected that would give the 'strange' result and I slowly get your explanation. Maybe I have some time left this weekend XD
Do you get the point, what I want to reach with this function? If yes, do you have a smart idea or can you explain short what I maybe can do better, more careful etc?
dpb on 4 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253954
Edited: dpb on 6 Dec 2014
Open in MATLAB Online
Primarily it looks like everywhere you have a gca you really want something that goes back to your original question and to probably try to set defaults for the just-created figure using
set(figure_handle,'DefaultLineLineWidth',2)
or the desired property/properties on the various other objects besides Line. You'll have to investigate which are and are not inherited from the figure; I don't know that otomh. In the initial query the linewidth property is; I've not investigated for the various font properties from whom they get derived. But, if you can't get to them from the figure then you'll have to have another level at which you create and save the right handles for the axes if you do want to make these dependent on specific figures and axes within those figures rather than global.
It's all doable; you just have to figure out where is the highest point in the object hierarchy you can do so. Try the figure first, obviously, but as soon as you use gca you've gone to an axes level, not figure so you'll have to see if it works substituting gcf first, then switch to the actual figure. If gcf fails because of invalid property then you've got to make the axes object and use (and save) that handle that's tied with the given parent figure.
Sign in to comment.
More Answers (2)
Robert Cumming on 3 Dec 2014
Open in MATLAB Online
set(0,'DefaultLineLineWidth',2)
This will set the line width for all plots in the current session
The technique your using in @dpb answer only sets the current axes (i.e. gca).
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Ruben on 3 Dec 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253602
Yeah, but I would have the freedom to choose the linewidth of the plotted object per figure... (A) So the answer @dpb is more suitable for my problem.
Sign in to comment.
Salvador Castaneda on 23 Oct 2021
This however, was great for mine, thanks
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsGraphics ObjectsGraphics Object Programming
Find more on Graphics Object Programming in Help Center and File Exchange
Tags
- linewidth figure function
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office