remove brace indexing from 1x1 matrix
조회 수: 1 (최근 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!