remove brace indexing from 1x1 matrix
2 ビュー (過去 30 日間)
古いコメントを表示
I have a list of equations written with a fprintf loop and imported with importdata:
6×1 cell array
{'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) '}
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) ' }
{'-0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) '}
{'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000)) ' }
using
A=importdata('equations list.txt');
B=transpose(A);
C=cell2mat(B);
I get the 1x1 matrix:
'-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000)) '
But i would like to remove the first and last ' ' to get this:
-0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) -0.0002578447+2*x(1)*0.7071067812*(0.0021272263)^2*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) -0.0003646474+2*x(1)*1.0000000000*(0.0021272263)^2*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000))
in order to use it later with fsolve.
Does anybody know how to do this?
1 件のコメント
Stephen23
2022 年 12 月 24 日
編集済み: Stephen23
2022 年 12 月 24 日
"Does anybody know how to do this?"
The single quotes are not part of the text, they are simply artifacts of the display routine (indicating the text is a character vector). It is not possible to remove what is not even there. You are confusing text with code.
As Jan correctly wrote, most likely you could simply write a function in an Mfile and supply that name/function handle of that function to FSOLVE. Then you can simply avoid the indirection of importing text and fiddling around with STR2FUNC.
採用された回答
Karim
2022 年 12 月 24 日
I would recommand to read the text file into a string array, this will ease the conversion into function. See below for a demonstration
% read the text file into a string array
F = readlines('equations list.txt')
% add '@(x)' to convert into functions
F = "@(x) " + F
% convert the strings into functions
F_eqn = cell(size(F));
for i = 1:length(F)
F_eqn{i} = str2func(F(i));
end
% have a look at the content
F_eqn
% evaluate function 2 with random input
x_test = rand(2,1);
my_sol = F_eqn{2}(x_test)
0 件のコメント
その他の回答 (1 件)
Jan
2022 年 12 月 24 日
The quotes are not part of the contents, but shown in the command window only to show, that this is a CHAR vector. Therefore there is no need to remove the quotes.
fsolve processes functions, not CHAR vectors. Instead of importing the file, it would be much easier to convert it to an m-function: Insert the formul here:
function y = fcn(x)
y = ... % your text here
end
and save this as fcn.m .
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!