Using bessel functions in MATLAB - excel

I'm trying to put all of my functions from Excel workbook into MATLAB. I'm having an issue using bessel functions in MATLAB. I'm simply not getting the same results from MATLAB as I do in excel.
For example, in excel if I execute
=0.32*BESSELI(0.32,0)/2/BESSELI(0.32,1)
I get 1.012.
When I use the same approach in MATLAB
0.32*besseli(0.32,0)/2/besseli(0.32,1)
I simply get zero.
Can someone please help me integrate bessel functions into my MATLAB script so that they provide the same answer as they do when used in excel?

MATLAB and Excel have the arguments of the besseli function in a different order.
The following expression (note the order of arguments changed):
0.32*besseli(0, 0.32)/2/besseli(1, 0.32)
will yield:
> ans = 1.0127
in MATLAB.

The documentation shows the formulae and show that if you use Z=0, which you have in your first besseli, you should get 0, which you do. The second call to besseli should not get you zero, and indeed it does not:
besseli(0.32,1)
ans =
1.0744
I copied the following from the aforementioned documentation:
This shows that unless your nu (that Greek thing that looks like a v) is zero, your modified Bessel function of the first kind at Z=0 will be, in fact zero.
On a side note: why are you doubly dividing and not simply writing
0.32*besseli(0.32,0)*besseli(0.32,1)/2

Related

How to concatenate two functions into one function?

I have a set of data that is generated:
=((E31/320)^2)/(2+(E31/380))
=((E32/320)^2)/(2+(E32/380))
=((E33/320)^2)/(2+(E33/380))
...
I want to create a sum of these, but I don't want to just SUM them together; I want to write a function that put these together. I came up with this row:
=SUMPRODUCT(((ROW(E1:INDEX(E31:E63;C34)))/320)^2/(2 + (E31:E63/380)))
The problem with this line is it seems to overdo the whole thing. I need to somehow use one variable for the both E31:E63 intervals, because it will otherwise loop through the second E31:E63 n-times, instead of using the same value.
As I see it, there are two solutions.
Write the data in columns, but using the first solution
Write the function, but try to find something that makes the two E31:E63 work as one variable.
I want to implement the second option.
I believe
=SUMPRODUCT(((E31:E63/320)^2)/(2+(E31:E63/380)))
Will do what you want.

MATLAB selecting items considering the end of their name

I have to extract the onset times for a fMRI experiment. I have a nested output called "ResOut", which contains different matrices. One of these is called "cond", and I need the 4th element of it [1,2,3,4]. But I need to know its onset time just when the items in "pict" matrix (inside ResOut file) have a name that ends with "*v.JPG".
Here's the part of the code that I wrote (but it's not working):
for i=1:length(ResOut);
if ResOut(i).cond(4)==1 && ResOut(i).pict== endsWith(*"v.JPG")
What's wrong? Can you halp me to fix it out?
Thank you in advance,
Adriano
It's generally helpful to start with unfamiliar functions by reading their documentation to understand what inputs they are expecting. Per the documentation for endsWith, it expects two inputs: the input text and the pattern to match. In your example, you are only passing it one (incorrectly formatted) string input, so it's going to error out.
To fix this, call the function properly. For example:
filepath = ["./Some Path/mazeltov.jpg"; "~/Some Path/myfile.jpg"];
test = endsWith(filepath, 'v.jpg')
Returns:
test =
2×1 logical array
1
0
Or, more specifically to your code snippet:
endsWith(ResOut(i).pict, 'v.JPG')
Note that there is an optional third input, 'IgnoreCase', which you can pass as a boolean true/false to control whether or not the matching ignores case.

Getting the result of an excel formula in python

I need to open a .xlsx-file (without writing to it) in python, to change some fields and get the output after the formulas in some fields were calculated; I only know the input fields, the output field and the name of the sheet.
To write some code: Here is how it would look like if I would have created the library
file = excel.open("some_file.xlsx")
sheet = file[sheet_name]
for k, v in input_fields.items():
sheet[k] = v
file.do_calculations()
print(sheet[output_field])
Is there an easy way to do this? Wich library should I use to get the result of the formulas after providing new values for some fields?
Is there a better way than using something like pyoo, maybe something that doesn't require another application (a python library is clearly better) to be installed?
I'll just thank you in advance.
I now came up with a (very ugly) solution.
I am now reading the xml within the xlsx-file, and I am now using eval and some regular expressions to find out wich fields are needed; and I have defined some functions to run the calculations.
It works, but it would be great if there were a better solution.
If the resulting library is ready, and I don't forget to do this; I'll add a link to the library (that'll be hosted on Github) to this answer to my own question.

Same for loop, giving out two different results using .write()

this is my first time asking a question so let me know if I am doing something wrong (post wise)
I am trying to create a function that writes into a .txt but i seem to get two very different results between calling it from within a module, and writing the same loop in the shell directly. The code is as follows:
def function(para1, para2): #para1 is a string that i am searching for within para2. para2 is a list of strings
with open("str" + para1 +".txt", 'a'. encoding = 'utf-8') as file:
#opens a file with certain naming convention
n = 0
for word in para2:
if word == para1:
file.write(para2[n-1]+'\n')
print(para2[n-1]) #intentionally included as part of debugging
n+=1
function("targetstr". targettext)
#target str is the phrase I am looking for, targettext is the tokenized text I am
#looking through. this is in the form of a list of strings, that is the output of
#another function, and has already been 'declared' as a variable
when I define this function in the shell, I get the correct words appearing. However, when i call this same function through a module(in the shell), nothing appears in the shell, and the text file shows a bunch of numbers (eg: 's93161), and no new lines.
I have even gone to the extent of including a print statement right after declaration of the function in the module, and commented everything but the print statement, and yet nothing appears in the shell when I call it. However, the numbers still appear in the text file.
I am guessing that there is a problem with how I have defined the parameters or how i cam inputting the parameters when I call the function.
As a reference, here is the desired output:
‘She
Ashley
there
Kitty
Coates
‘Let
let
that
PS: Sorry if this is not very clear as I have very limited knowledge on speaking python
I have found the solution to issue. Turns out that I need to close the shell and restart everything before the compiler recognizes the changes made to the function in the module. Thanks to those who took a look at the issue, and those who tried to help.

Problems using matlab to print a histogram to a file

I am trying to create a histogram of numbers in an array. I am using Matlab to do this. I am connecting via ssh so I can only use Matlab in the terminal on my Linux computer. I am trying to create a histogram of the data in the array and save it as a .png. I know that in order for me to save this I need to use the print function. So far my attempt has been the following:
h=hist(array)
print(h,'-dpng','hist1.png')
which told me that there is no variable defined as -dpng but I thought that the point of that was to specify the file type.
Then I just deleted the -dpng and ran it as
print(h,'hist1.png')
to which it told me "Handle must be scalar, vector, or cell-array of vectors"
At this point I don't quite know what to do next. I would like for someone to help me figure out how to print this histogram to a .png file. Thank you.
hist does not return a figure handle, you could to do something similar to:
h = figure;
hist(array);
print(h, '-dpng', 'hist1.png');
to save the histogram.
By itself, the function hist(array) plots a histogram. If you assign the output to a variable, it returns the binned values of array, not the handle to your plot.
f = figure;
hist(array)
saveas(f,'hist.png')
you may would like to output the array to a csv file.
fid = fopen('file.csv','wt');
for i=1:size(arr)
fprintf(fid, '%s,%d,%d\n','element number' ,i ,arr(i));
end
fclose(fid);
See this link, you should be able to change the answers there to your needs: Outputing cell array to CSV file ( MATLAB )
You don't need to use figure handle unless you want to print not current figure. By default print uses gcf that returns handle for current figure.
So you just can do:
hist(array)
print('-dpng','hist1.png')
You got an error that there is no variable defined as -dpng probably because you forgot one quote symbol and used -dpng'.

Resources