Hi
I've got a text document and i would like to organize the data so that i could insert it in a table(SQL)
The 1st line is the number of organizations, the 2nd is the name of the first organization, the 3rd is the number of countries and then the countries until the next organization.
Does anyone have a clue on how this can be done?
Thanks

2 件のコメント

Star Strider
Star Strider 2014 年 12 月 12 日
Attaching the text file would help. (Use the ‘paperclip’ or ‘staple’ icon just to the left of the (?)Help button.)
Manel Carreira
Manel Carreira 2014 年 12 月 12 日
Done

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2014 年 12 月 12 日

1 投票

To read your file as a matlab table
c = cell(0, 2);
fid = fopen('somefile.txt', 'rt'); %open file for reading in text mode
numorganisations = str2double(fgetl(fid));
for org = 1:numorganisations
orgname = fgetl(fid);
numcountries = str2double(fgetl(fid));
for country = 1:numcountries
countryname = fgetl(fid);
c(end+1, :) = {orgname, countryname};
end
end
fclose(fid);
t = cell2table(c, 'VariableNames', {'Organisation', 'Country'})

1 件のコメント

Manel Carreira
Manel Carreira 2014 年 12 月 12 日
Thank you

サインインしてコメントする。

その他の回答 (1 件)

Thorsten
Thorsten 2014 年 12 月 12 日

1 投票

fid = fopen('organizacoes.txt');
line = fgets(fid); i = 1;
while line ~= -1
T{i} = line(1:end-2); % end-2 to get rid of LF CR
line = fgets(fid); i = i + 1;
end
fclose(fid)
i = 2;
organization = {};
country = {};
while i <= size(T, 2)
n = double(T{i+1}) - double('0');
organization(end+1:end+n) = {T{repmat(i, [1 n])}};
country(end+1:end+n) = {T{i+2:i+2+n-1}};
i = i + 2 + n; % next organization
end

1 件のコメント

Guillaume
Guillaume 2014 年 12 月 12 日
編集済み: Guillaume 2014 年 12 月 12 日
Thorsten,
  1. it is better to open text file with 'rt', so matlab can deal properly with line endings on different platform. You only need to deal with '' on all platforms. '\r' is appended / removed automatically for you on Windows.
  2. If you're going to strip the line endings anyway, use fgetl instead of fgets as fgetl does it for you.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

タグ

質問済み:

2014 年 12 月 12 日

編集済み:

2014 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by