Putting fit equation on plot in one line - matlab

I am using a curve fit and I would like to display the equation in the plot. I tried using a text but therewith the text is splitted and not in one line. That is probably due to the blanks before and after each variable. Is there a way to have the text displayed in one single line?
Here's my code:
pars=fit(time_s,p_bar,'exp2');
plot(pars);
a = num2str(pars.a);
b = num2str(pars.b);
c = num2str(pars.c);
d = num2str(pars.d);
hold on;
txt = {'p(t) = ' a '*exp(' b '*t) + ' c '*exp(' d '* t)'};
text(150,2,txt);
Thank you in advance!

txt = strcat(['p(t) = ', a, ' * exp(', b, ' * t) + ', c, ' * exp(', d, ' * t)']);
this works

Related

How can I create title with variable using Latex in Matlab?

I have a Matlab code to generate a title representing the multiplication of two fractions using Latex.
t=title('$\frac{5}{2} \times \frac{3}{4}$');
set(t,'Interpreter','Latex');
My question is how to replace the numbers in Latex equation to variables?
For example, if I defined
A = 5;
B = 2;
C = 3;
D = 4;
How to use A,B,C,D to replace the numbers on the latex form title?
You will need to either use [] to concatenate the string
titlestr = ['$\frac{', num2str(A), '}{', num2str(B), '} \times \frac{', num2str(C), '}{', num2str(D)'}$'];
title(titlestr)
Or you can use sprintf but you'll have to be sure to escape all of the \ characters
titlestr = sprintf('$\\frac{%d}{%d} \\times \\frac{%d}{%d}$', A, B, C, D);
title(titlestr)

Matlab: func2str from a function in a m-file

In a main m-file I have
conformal = maketform('custom', 2, 2, [], #conformalInverse_0001, []);
used in imtransform that refers to the function defined in conformalInverse_0001.m:
function U = conformalInverse_0001(X, ~)
%#codegen
U = [zeros(size(X))];
Z = complex(X(:,1),X(:,2));
W = 1./(4.*Z.^2-1);
U(:,2) = imag(W);
U(:,1) = real(W);
How can I get the string '1./(4.*Z.^2-1)' in the main program?
I found a way to solve it, but it's not so elegant...
Assume conformalInverse_0001.m is a file in your folder.
You can parse the file as a text file, and search for your formula.
Example:
Assume you know the location is 5'th line in file, and start with W =.
You can use something like the following code to read '1./(4.*Z.^2-1)' in the main program:
%Open file for reading.
fid = fopen('conformalInverse_0001.m', 'r');
%Read 5 lines.
s = textscan(fid, '%s', 5, 'delimiter', '\n');
fclose(fid);
%Get the 5'th line.
s = s{1}(5);
%Convert cell array to string.
s = s{1};
%Get characters from the 5'th character to one char before end of string.
s = s(5:end-1)
Result: s = 1./(4.*Z.^2-1)
You can check textscan documentation for finding more elegant solution.
I'm not sure I fully understand the problem here, but what about adding into your conformalInverse_0001 function something like:
str = '1./(4.*Z.^2-1)';
save('temp_str','str') % or whatever data that you want to save from it
and then adding in your main file:
load('str.mat')% or you can use 'impordata'
where you want to extract it.
I have hacked two solutions with textscan: first knowing the line number and second searching the line that starts with substring 'W = '
% read line line_num = 5 and process string
f_id = fopen(conformalInverse_m_path);
conformalInverse_cell = textscan(f_id,'%s','delimiter','\n'); %disp(conformalInverse_cell); % {68×1 cell}
func_string = conformalInverse_cell{1}{line_num}; disp(func_string); % W = 1./(4.*Z.^2-1); OK
func_string_2=func_string(5:end-1); disp(func_string_2); % 1./(4.*Z.^2-1); OK
% read first line that starts with substring 'W = ' and process string
W_string = 'W = ';
for i=1:100
func_string = conformalInverse_cell{1}{i};
Firt4=func_string(1:4); %disp(['i = ', num2str(i), ': First4 = ', Firt4]);
if strcmp(Firt4,W_string) == 1; line_nr = i; break; end;
end
func_string_2 = conformalInverse_cell{1}{line_nr};
func_string_3=func_string_2(5:end-1);

How could this file be stored in matlab

I have this excel file attached, I am wondering how I could store it in matlab such that I could refrence it as A.AX, B.BY etc. What is the fastest way to do this. Or could I convert the excel file to a matlab file?
I saw that you already accepted, but here is another option
data = xlsread('Book1.xlsx');
a = data(:,1:4) ;
b = data(:,6:9) ;
c = data(:,11:14) ;
A = cell2struct( num2cell(a, 1) , {'AW', 'AX', 'AY', 'AZ'}, 2);
B = cell2struct( num2cell(b, 1) , {'BW', 'BX', 'BY', 'BZ'}, 2);
C = cell2struct( num2cell(c, 1) , {'CW', 'CX', 'CY', 'CZ'}, 2);
One way to do this is to import each column in row 3 of your doc as a matrix and then format them into tables named(A, B, C...)
For example:
AW = [148;174;177;217;280;145;291]; % Entries in column AW
AX = [376;360;553;390;464;359;411]; % Entries in column AX
A = table(AW,AX); % AW and AX are put into table named A
You can then easily acces each value or column by for example A.AW(1) or A.AW(:) etc.
The final table looks like this:

Matlab, Basics about Loops

I need to ask the user for a temperature, k or K stands for kelvin, c or C stands for celsius and f or F stands for Fahrenheit. How can I put all of those in a loop? I need to keep asking the user until they enter in one of the letters above. This is what I have so far.
tempType = input('What type of temperature would you like to use: ', 's');
value = ['k','K','c','C','f','F'];
while strcmp(tempType, value) == 1
tempType = input('What type of temperature would you like to use: ', 's');
end
I'd rather do like this to avoid two exactly same lines (tested in R2011b):
AllowedTemperature = {'k','K','c','C','f','F'};
SelectedTemperature = '';
while ~any(strcmp(SelectedTemperature,AllowedTemperature))
SelectedTemperature = input('What type of temperature would you like to use: ', 's');
end
disp( [ 'SelectedTemperature: ' SelectedTemperature ] )
You want (length(tempType)~=1 || length(findstr(tempType,value))==0) to be the condition of your while

Function, in MATLAB dna replication

I'm trying to figure out the function command in Matlab and im having some difficulties.
I'm trying to write a Matlab function named dna_replicate. It will replicate a given strand and return its partner strand
For example if the user enters ATGCATGCAHGCAGTC, it should return TACGTACGT CGTCAG
A-->T
G-->C if the user enters other than these 4 letters, there should be blank in the partner strand. Thank you for your help
This implementation should be faster, involving only a simple table look-up. Note that the table t is constructed only once when the function is first called.
function out = dna_replicate(in)
persistent t
if isempty(t)
t = blanks(256);
t('ATGC') = 'TACG';
end
out = t(in);
end
How about:
function out = dna_replicate(in)
in = upper(in); % Ensures all same case
A = in=='A';
T = in=='T';
C = in=='C';
G = in=='G';
out = in;
out(A) = 'T';
out(T) = 'A';
out(C) = 'G';
out(G) = 'C';
out(~(A|T|C|G)) = ' ';
while #Jirka cigler answer works, it uses a for loop as well as dynamically growing vector 'out'. As matlab is optimized for vector operations, this answer should perform better.
You can create a simple vectorized solution using the function ISMEMBER:
function outString = dna_replicate(inString)
[~,index] = ismember(upper(inString),'ACGT'); %# Find the indices of inStrings
%# letters in string `ACGT`
outString = 'ACGT '; %# Initialize outString to `ACGT` and a blank
outString = outString(5-index); %# Use inverted and shifted index to expand
%# outString to the size of inString
end
And here's a test:
>> dna_replicate('ATGCATGCAHGCAGTC')
ans =
TACGTACGT CGTCAG
I think it can be implemented as follows:
function out=dna_replicate(in)
for i=1:numel(in)
switch in(i)
case 'A'
out(i)= 'T';
case 'G'
out(i)= 'C';
case 'T'
out(i)='A';
case 'C'
out(i)='G';
otherwise
out(i)=' ';
end
end
this function has argument of type char
in='ATGCATGCAHGCAGTC'
and you can run
out=dna_replicate(in)
to get the same result as you want :-)