Why am I not getting a log scale on my x-axis?

54 ビュー (過去 30 日間)
James Leak
James Leak 2022 年 7 月 15 日
コメント済み: dpb 2022 年 7 月 15 日
Hi there,
I have code here to analyse some data, I need two of the plots in a semi-log scale on the x-axis. The following code has worked for one figure:
% G vs. Axial
figure
scatter(x,x3,'filled','o',Color="black")
set(gca,'xscale','log')
hold on
scatter(x4,x7,'filled','o',Color="blue")
hold on
scatter(x8,x12,'filled','o',Color="red")
hold on
scatter(x14,x17,'filled','o',Color="green")
hold on
scatter(x18,x21,'filled','o',Color="magenta")
hold on
scatter(x22,x25,'filled','o',Color="cyan")
hold on
xlim([0.01 100])
However, this does not work for the next figure with similiar code:
figure
plot(y2,y3,Color="black",LineStyle="-",LineWidth=1.5)
set(gca,'xscale','log')
hold on
plot(y6,y7,Color="blue",LineStyle="-",LineWidth=1.5)
set(gca,'xscale','log')
hold on
plot(y11,y13,Color="red",LineStyle="-",LineWidth=1.5)
set(gca,'xscale','log')
hold on
plot(y16,y17,Color="green",LineStyle="-",LineWidth=1.5)
set(gca,'xscale','log')
hold on
%plot(y20,y21,Color="yellow",LineStyle="-",LineWidth=1.5)
%set(gca,'XScale', 'log')
hold on
plot(y24,y25,Color="cyan",LineStyle="-",LineWidth=1.5)
set(gca,'xscale','log')
ylim([0.4 0.7])
Where am I going wrong? Is this a problem with the hold on handle?
Thank you in advance!
Kind regards,
James
  7 件のコメント
Adam Danz
Adam Danz 2022 年 7 月 15 日
I can confirm that attaching an xlsx file was successful on our end. It is very large? Corrupt or not actually an xlsx file? Maybe the ol' close-and-reopen browser trick would help? Sorry you're having this problem.
Steven Lord
Steven Lord 2022 年 7 月 15 日
What does "does not work" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2022 年 7 月 15 日
編集済み: dpb 2022 年 7 月 15 日
Has nothing to do with log or scale but
plot(y2,y3,Color="black",LineStyle="-",LineWidth=1.5)
is bad syntax -- the error message should have been explanatory that it never got to the next line--
>> y2=1:10;y3=randi(15,size(y2));
>> plot(y2,y3,Color="black",LineStyle="-",LineWidth=1.5)
plot(y2,y3,Color="black",LineStyle="-",LineWidth=1.5)
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
>>
Optional parameters to plot, like all other MATLAB functions are name,value pairs, no assignment operator allowed and the parameter names must be string or char() strings; you've put in what look like variable names to the parser..
plot(y2,y3,"Color","black","LineStyle","-",'LineWidth',1.5)
I'd then recommend changing your coding style somewhat from your example code above; in particular instead of using gca repeatedly that is a function, retrieve the current handle and use it. The "dot" operators are also more convenient than set() for single parameters...
hAx=gca;
hAx.XScale='log';
And, of course, you can get what you want directly by using
semilogx(y2,y3,'k-','LineWidth',1.5)
Although we don't have enough auxiliary code to be able to see, I'll also note that having a "veritable plethora" of sequentially-named variables such as you show here is often a key sign that the power of MATLAB isn't being used by using array syntax. With such you can generally then write generic code instead of having to duplicate the same code over and over with the different variables explicitly, as shown above.
  4 件のコメント
Steven Lord
Steven Lord 2022 年 7 月 15 日
May I be so rude as to ask "WHY?"
User requests for functionality similar to this (there are two just in the "What is missing from MATLAB" discussion: 1, 2). I suspect there have also been requests for this functionality reported directly to Technical Support, though I'm not sure offhand how many.
Another nice feature of this, IMO, is that setting parameters in a plot call now looks an awful lot like setting parameters after-the-fact:
y2 = 1:10;
y3 = y2.^2;
plot(y2,y3,Color="black",LineStyle="-",LineWidth=1.5)
figure
y2 = 1:10;
y3 = y2.^2;
h = plot(y2,y3);
h.Color="black";
h.LineStyle="-";
h.LineWidth=1.5;
Most of the work converting the former code to the latter was replacing commas with "h." and moving a right parenthesis.
How much code complexity and slowdown comes with the introduction of yet another asymmetric syntax quirk?
I believe little to none. I don't have the timing data right at hand, but I know that was analyzed during development.
And how much time spent in development to implement it for existing functions that could have gone to new features and/or optimizing existing performance bottlenecks?
You may prefer that the development time that was spent on this had gone to "new features". There are likely users who, if this development time had gone to those "new features" would have preferred it to be spent on this functionality instead.
You're also implicity assuming that the developers who implemented this functionality would be able to develop the new features that you want or to optimize the code bottlenecks in which you're interested. That's not always a valid assumption. I'd take longer to test a feature in Simulink than someone who has the same amount of experience testing Simulink as I do testing MATLAB, as one example.
It seems more and more effort goes towards "fluff" and the expense of code bloat and performance.
One person's "fluff" could be another person's ladder helping them climb the learning curve more easily. And sometimes a new feature can avoid or reduce "code bloat". One example that I was reminded of when looking through that "What is missing from MATLAB" discussion was the page functions: pagemldivide, pagemrdivide, pagemtimes, etc.
If you wanted to multiply pages of two 3-dimensional arrays before pagemtimes, you'd probably want to preallocate the result then use a for loop to loop over the pages of the inputs. Let's say four lines of code including the end ending the for loop. Now it's one line, a call to pagemtimes.
Is pagemtimes "fluff"? It's just replacing a for loop. Is it "bloat"? It does increase the size of the MATLAB libraries on disk (slightly.) Is it useful? I think so.
dpb
dpb 2022 年 7 月 15 日
Well, while misguided imo, that being the case then there's certainly nothing in what has been shown that would seem to be an issue -- so it's in something that isn't.
One possibility that although it wouldn't prevent changing the axes to log scale would be if the x data were 0 and/or negative...nothing would show on the plot -- but there should also then be a warning message.
Well, huh! That's new behavior, too -- it now plots the data on a log scale with a minus sign -- hadn't ever tried that since whenever that was introduced. But, that doesn't then explain an issue OP is having, either.
Not sure we can duplicate unless get a complete minimum example that is supposed to exhibit the symptom.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by