How to upload an array of double in postgreSQL?

조회 수: 1 (최근 30일)
Atanu
Atanu 2022년 7월 17일
답변: Ramtej 2023년 9월 6일
I have a table in which different columns contain different types of data. I want to upload the table to postgreSQL database. I am having trouble with uploading the columns where datatype is cell. If I convert them to string or double(cell2mat) it's still not working. Here is my code:
datasource = 'live_database';
conn = database(datasource,'postgres','1234');
loadfile = load("Outputs1to70974.mat");
data = loadfile.allOutputs;
data.id = string(data.id); %1
data.subjectid = string(data.subjectid); %2
data.trialname = string(data.trialname); %3
data.date = string(data.date); %4
data.logical_out = string(data.logical_out); %5
data.run_time = cell2mat(data.run_time); %6
data.reaction_time = cell2mat(data.reaction_time); %7
data.average_position = cell2mat(data.average_position); %8
data.position_when_offered = cell2mat(data.position_when_offered); %9
tablename = "toytable";
sqlwrite(conn,tablename,data);
The sqlwrite documentation says numeric array type should is valid to upload on database, but seems like it's not working for me. How should I troubleshoot?

답변 (1개)

Ramtej
Ramtej 2023년 9월 6일
Hi,
I can see that "data.average_position" is a numeric matrix of size (10,2). You cannot insert an array of multiple columns into a single column in the database.
Create two separate columns for 'x' position data and 'y' position data, and upload them to the database.
%%
data.average_position = cell2mat(data.average_position);
data.avg_Xposition = data.average_position(:,1); % array of avg x postition values
data.avg_Yposition = data.average_position(:,1); % array of avg y postition values
data.average_position = [] % clear the old position matrix
%% follow the above routine for all the applicable columns
Hope this resolves your query!

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by