Effacer les filtres
Effacer les filtres

is it possible to use matlab to produce tuples in python?

3 vues (au cours des 30 derniers jours)
Michael
Michael le 20 Oct 2012
Réponse apportée : surya venu le 19 Juil 2024 à 9:30
i've been using this to write variables into a .py file then opening this file in the main script (the script creates a model in the finite element solver ABAQUS and python is the only language it understands)
GridSpaceX=1;
%Make python file with variables
delete('Var.py');
fid = fopen('Var.py', 'w');
fprintf(fid,'GridSpaceX = %0.12f\n',GridSpaceX);
but for one of the variables I need to create a python tuple and all i have is a matlab matrix. any help would be greatly appreciated. Thanks for your help, Michael p.s. a tuple is an immutable list and the matrix is a 2 by lots matrix that is created by matlab

Réponses (1)

surya venu
surya venu le 19 Juil 2024 à 9:30
Hi,
You can write a MATLAB script to convert a MATLAB matrix into a Python tuple and then write that tuple to a ".py" file. Below is an example of how you can achieve this.
GridSpaceX = 1;
Matrix = [1, 2, 3; 4, 5, 6];
% Convert the matrix to a Python-compatible tuple string
tuple_str = '(';
for i = 1:size(Matrix, 2)
tuple_str = strcat(tuple_str, '(', num2str(Matrix(1, i)), ',', num2str(Matrix(2, i)), '),');
end
tuple_str(end) = ')';
% Make python file with variables
delete('Var.py');
fid = fopen('Var.py', 'w');
fprintf(fid, 'GridSpaceX = %0.12f\n', GridSpaceX);
fprintf(fid, 'MatrixTuple = %s\n', tuple_str);
fclose(fid);
Hope it helps.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by