Convert from linear to log scale and back again

44 views (last 30 days)
I have include some example data.
%fit curve
[xData, yData] = prepareCurveData(log10(plot_data.xdata), plot_data.ydata);
ft = fittype( 'poly1' );
[fitresult, gof] = fit( xData, yData, ft ); %fit model to data
ci=confint(fitresult,0.95);
sp1=plot(fitresult,xData,yData,'ob'); %plot
I have distance on the x-axis, and sound pressure level on the y-axis. The distances are originally in metres, but when I fit a best fit line, the equation I use needs them to be logged. My issue is that I want to convert the x-axis back to metres so it's in a readable format. In addition, I'd like this to be on a logarithmic scale.
I tried the following:
xticklabels({'','','100','','','','','1000'}) %re-label x ticks in metres
set(gca,'XScale','log')
but the x-axis doesn't appear to be logged at all when I do this and the 1000m value disappears when I set the scale to log.

Answers (1)

Jon
Jon on 23 Nov 2021
I think you can get what you want using MATLAB's semilogx function
  21 Comments
Jon
Jon on 25 Nov 2021
Edited: Jon on 25 Nov 2021
Sorry, I don't have the curve fitting toolbox, so I can't try running your exact code to see if I can replicate the problem. I'm pretty sure the plot3.png that you attached is not the output from your code above, maybe some confustion in the files or something like that. For me using semilogx always produces a log spaced output and your plot3.png has a linear one. As I showed you a few post ago, when I make a ordinary numerical vectors xData, and yfit, and and run the last three lines of code above I get a log spaced x axis with labels 10,100,...
Just to be clear I will post the example again here.
x = 1:1000
yfit = x.^2
semilogx(x,yfit),
get(gca,'xtick')
set(gca,'XTickLabel',New_XTickLabel)
Which produces
Is this the kind of plot you are hoping to get? When you run the above code do you get the same plot I show above
If so, I can't really explain why you don't get the same result with your code and I guess I can't offer you any more help. If the example above, with made up data gives the plot you want, and when you assign the output using fitresult(xData) it doesn't work, then either there is some slight but important difference between our two codes, or maybe there is some aspect of using the curve fit toolbox that I am not aware of, but I'm mystified and frustrated I couldn't get this working for you. Sorry, I hope you can figure it out soon.
Louise Wilson
Louise Wilson on 25 Nov 2021
Yes that is definitely the output. That is why I included a few comments back, the screenshot of running the code and the resultant figure, so you could believe it. I will start a new question.

Sign in to comment.

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!