Undefined function 'log' for input arguments of type 'uint8' - matlab

i have been trying generate an image.
C1 = imread(InputImage);
NumberOfGrayLevels=32;
I= 0.299*C1(:,:,1)+0.587*C1(:,:,2)+0.114*C1(:,:,3);
C = 0;
I=(C*log(I+1))';
new=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
[m,n]= size(new);
rgb = zeros(m,n,3);
rgb(:,:,1) = new;
rgb(:,:,2) = rgb(:,:,1);
rgb(:,:,3) = rgb(:,:,2);
new = rgb/256;
imshow(new,[]);
no9=figure;
image(new);
the error is showing at I=(C*log(I+1))';..can you tell me how to solve this?

Most probably C1 is of type uint8. You should convert it, i.e.:
C1 = imread(InputImage);
C1 = double(C1);
NumberOfGrayLevels = 32;
I = 0.299*C1(:,:,1) + 0.587*C1(:,:,2) + 0.114*C1(:,:,3);
.....
if you don't convert C1 to double, then I will also be of type uint8 - it will not contain what you want and log function will not work with it.

Related

Can someone help me with the update function in Matlab to move values to MySQL?

I'm dealing with a problem with the update function in Matlab.
conn=database('MySQL','user','password');)
selectquery_select = 'SELECT * FROM inputs WHERE i_read = 0';
data_select = select(conn,selectquery_select);
for j=1:size(data_select)
id_data = data_select(j,1);
id_data = string(id_data.(1));
time_data = data_select(j,4);
time_data = string(time_data.(1));
time_dataform = datetime(time_data,'InputFormat','yyyy-MM-dd HH:mm:ss');
y0=data_select(j,2);
y0 = str2num(string(y0.(1)));
r0=data_select(j,3);
r0 = str2num(string(r0.(1)));
if id_data == "115"
run("C:\Users\...\uu.m")
update(conn,'inputs','i_read',1,'WHERE (ID_code = "115") AND WHERE (i_Time = time_data)');
end
end
Basically, I'm taking some value from the database when i_read is equal to 0 (i_read is a boolean variable in the database that should give 1 if the value is already processed and 0 if not). After a value is read, we want to change the i_read in the database from 0 to 1. We decide to use the update function, but this gave us the following error:
Error using database.odbc.connection/update
Too many input arguments.
Error in Patient_Identification (line 57)
update(conn,'inputs','i_read',1,'WHERE (ID_code = "112") AND WHERE (i_Time = ', time_data,')');
Someone is able to help us with this problem? Thank you.

Unable to read file 'subjFiles(1).name'. No such file or directory

I'm using some codes that called specific function.
in line 17 of this function I get the error
Index exceeds matrix dimensions.
Error in generateExpReport (line 17)
checkPValuesField = subjFiles(1).name;
the function up to line 20 is like this:
function [] = generateExpReport(copyDir,resultDir,params)
% Syntax
methodNames = fieldnames(params.methods);
numMethods = length(methodNames);
for i = 1 : numMethods
cd(resultDir)
numTargets = length(params.methods.(methodNames{i,1}).idTargets);
idDrivers = params.methods.(methodNames{i,1}).idDrivers;
nameFiles = [methodNames{i,1} '*.mat'];
subjFiles = dir(nameFiles);
numSubj = length(subjFiles);
significanceOnDrivers = zeros(numSubj,numTargets);
matrixTransferEntropy = zeros(numSubj,(numTargets)+1);
% check if the pValues matrix is present
checkPValuesField = load(subjFiles(1).name);
fields = fieldnames(checkPValuesField);
nameFields = checkPValuesField.(fields{1,1});
I can't find the problem
please help me:(( what's wrong with
checkPValuesField = load(subjFiles(1).name);

matlab oop "use data of hole class" for calculation

First, sorry for the bad title - I'm new to OO programming - basically I'd like to understand how Matlab works with oop classes.
Before I ask my question, here is a basic example of what I want to do:
I have two houses and some data about them and I got the idea, to work with oop classes. Here is my .m file.
classdef building
properties
hohe = 0;
lange = 0;
breite = 0;
xabstandsolar = 0;
yabstandsolar = 0;
end
methods
function obj = building(hohe, lange, breite, xabstandsolar, yabstandsolar)
obj.hohe = hohe;
obj.lange = lange;
obj.breite = breite;
obj.xabstandsolar = xabstandsolar;
obj.yabstandsolar = yabstandsolar;
end
function hohenwinkel(h)
h = h
d = sqrt(obj.xabstandsolar^2 + yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
end
end
I filled it with some data - for example
>>H1 = building(10,8,6,14,8)
>>H2 = building(18,8,6,14,0)
And now I want to calculate "gamma_v" (as an 1x2 array) for each building. Any ideas, how I can archive this?
Edit:
I want to create gamma_v (as an array) automatically for all objects in the class "building". There will be a lot more "houses" in the final script.
Your hohenwinkel method needs to accept two input arguments. The first argument for non-static methods is always the object itself (unlike C++, you'll have to explicitly list it as an input argument) and the second input will be your h variable. You'll also want to actually return the gamma_v value using an output argument for your method.
function gamma_v = hohenwinkel(obj, h)
d = sqrt(obj.xabstandsolar^2 + obj.yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
Then you can invoke this method on each building to get the value
gamma_v1 = hohenwinkel(H1);
gamma_v2 = hohenwinkel(H2);
If you want to have an array of buildings, you can create that array
houses = [building(10,8,6,14,8), building(18,8,6,14,0)];
gamma_v = hohenwinkel(houses);
and then construct your hohenwinkel function to operate on each one and return the result
function gamma_v = hohenwinkel(obj, h)
if numel(obj) > 1
% Compute hohenwinkel for each one
gamma_v = arrayfun(#(x)hohenwinkel(x, h), obj);
return
end
d = sqrt(obj.xabstandsolar^2 + obj.yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
there is some tricky solution (and its not recommended)(#Suever solution is better)
you should create a handle class
classdef gvclass<handle
properties
gvarr=[];
end
methods
function setgvarr(obj,value)
obj.gvarr=[obj.gvarr,value];
end
end
end
then use this class in your building class
classdef building<handle
properties
gv
hohe = 0;
lange = 0;
breite = 0;
xabstandsolar = 0;
yabstandsolar = 0;
end
methods
function obj = building(hohe, lange, breite, xabstandsolar, yabstandsolar,handle_of_your_gv_class,h)
obj.hohe = hohe;
obj.lange = lange;
obj.breite = breite;
obj.xabstandsolar = xabstandsolar;
obj.yabstandsolar = yabstandsolar;
obj.gv=handle_of_your_gv_class;
obj.hohenwinkel(h);
end
function hohenwinkel(obj,h)
d = sqrt(obj.xabstandsolar^2 + yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
obj.gv.setgvarr(gamma_v);
end
end
end
finally before creating any building you should create an object of gv class and pass it to the building constructor,
Egv=gvclass();
H1 = building(10,8,6,14,8,Egv,2)
H2 = building(18,8,6,14,0,Egv,3)
and to access gv array:
Egv.gvarr
you should do more effort on this issue to debug possible errors.

Fit with the parameter

I am quite new to Matlab and I am trying to use this code I found online.
I am trying to fit a graph described by the HydrodynamicSpectrum. But instead of having it fit after inputting fvA and fmA, I am trying to obtain the fitted parameters for this value also.
I have tried removing them, changing them. But none is working. I was wondering if any one here will be able to point me into the right direction of fixing this.
specFunc = #(f, para)HydrodynamicSpectrum(f, [para fvA fmA]);
[fit.AXfc, fit.AXD] = NonLinearFit(fit.f(indXY), fit.AXSpec(indXY), specFunc, [iguess_AXfc iguess_AXD]);
[fit.AYfc, fit.AYD] = NonLinearFit(fit.f(indXY), fit.AYSpec(indXY), specFunc, [iguess_AYfc iguess_AYD]);
[fit.ASumfc, fit.ASumD] = NonLinearFit(fit.f(indSum), fit.ASumSpec(indSum), specFunc, [iguess_ASumfc iguess_ASumD]);
predictedAX = HydrodynamicSpectrum(fit.f, [fit.AXfc fit.AXD fvA fmA]);
predictedAY = HydrodynamicSpectrum(fit.f, [fit.AYfc fit.AYD fvA fmA]);
predictedASum = HydrodynamicSpectrum(fit.f, [fit.ASumfc fit.ASumD fvA fmA]);
function spec = HydrodynamicSpectrum(f, para);
fc = para(1);
D = para(2);
fv = para(3);
fm = para(4);
f = abs(f); %Kludge!
spec = D/pi^2*(1+sqrt(f/fv))./((fc - f.*sqrt(f./fv) - (f.^2)/fm).^2 + (f + f.*sqrt(f./fv)).^2);
function [fc, D, sfc, sD] = NonLinearFit(f, spec, specFunc, init);
func = #(para, f)spec./specFunc(f, para);
[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);
fc = paraFit(1);
D = paraFit(2);
ci = nlparci(real(paraFit), real(resid), real(J)); % Kludge!!
sfc = (ci(1,2) - ci(1,1))/4;
sD = (ci(2,2) - ci(2,1))/4;
[paraFit, resid, J] = nlinfit(f, ones(1, length(spec)), func, init);
It looks like you get your fitted parameter using this line. And you are further processing them to get other stuff out from the function. You can modify your second function to get them out as well.
As there are very few comments, and questions seems to be application specific, there is not much help I can give with what you have presented.

Matlab name assignment

I am trying to decrease some big chucks of matlab code i had from a while ago, and was hoping to get them a bit more "clean".
The VarName2,VarName3,VarName4 ...etc are provide by measured data and i will know what they are always going to be thus i gave me the name A,B ,C , the think i want changed though is the first part of the name, so every time i run the .m file I will use the input('') option
where as fname = 'SWAN' and A, B , C are the second part of the name and they are constant.
fname = input ('enter name')
fname_A = VarName2
fname_B = VarName3
fname_C = VarName4
and want to be getting
SWAN_A = VarName2
SWAN_B = VarName3
SWAN_C = VarName4
thank you
Following your advices I been trying the structure construction
S.name = input ('enter name of the data ".." ==')
S.A = A;
S.A(1,:)=[];
S.B = B;
S.B(1,:)=[];
S.C = C;
S.C(1,:)=[];
S.D = D;
S.D(1,:)=[];
S.E = E;
S.E(1,:)=[];
may i ask if i can also have an input thing command so i can change the name of the structure?
Precede the script with S='west' and then do
'S'.name = input ('enter name of the data ".." ==')
S.A = A;
Here is how I would probably store the information that you are handling:
S.name = input ('enter name')
S.A = VarName2
S.B = VarName3
S.C = VarName4
And if you want to do it a few times:
for t=3:-1:1
S(t).name = input ('enter name')
S(t).A = VarName2
S(t).B = VarName3
S(t).C = VarName4
end
In this way you could now find the struct named 'swan':
idx = strcmpi({S.name},'SWAN')
you can use eval
eval( sprintf('%s_A = VarName2;', fname ) );
eval( sprintf('%s_B = VarName3;', fname ) );
eval( sprintf('%s_C = VarName4;', fname ) );
Note that the use of eval is not recommended.
One alternative option may be to use struct with dynamic field names:
A.( fname ) = VarName2;
B.( fname ) = VarName3;
C.( fname ) = VarName4;
Now you have three structs (A, B and C) with A.SWAN equal to VarName2, B.SWAN equal to VarName3 etc.