1
字符串之間插入空格我有這個字符串文本文件我需要多行文本文件MATLAB
123456789
987654321
111111111
222222222
我如何在文本文件中插入每個號碼之間的1個空間?
對於這一點的說:
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
字符串之間插入空格我有這個字符串文本文件我需要多行文本文件MATLAB
123456789
987654321
111111111
222222222
我如何在文本文件中插入每個號碼之間的1個空間?
對於這一點的說:
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
s=['123456789'
'987654321'
'111111111'
'222222222'];
s2=repmat(' ',size(s,1),2*size(s,2));
s2(:,1:2:end)=s
會給你
s2 =
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
更新:
使用dlmwrite
到s
保存到這樣的空間分隔的文本文件:
dlmwrite('testData.txt',s-'0',' ');
當字符矩陣s
被減去'0'
字符時,將被轉換爲0-9範圍內的數字數組。請參閱gnovice的solution,以便在同一文件中進行讀取,處理和加載。
這裏是你如何閱讀,處理和輸出數據到同一個文件:
fid = fopen('your_file.txt', 'r+'); % Open for both reading and writing
data = fscanf(fid, '%c', Inf); % Scan all contents into a character vector
data = regexprep(data, '\S', '$0 '); % Insert space after all non-whitespace
fseek(fid, 0, -1); % Move file pointer to beginning of file
fprintf(fid, '%c', data); % Output data
fclose(fid); % Close file
打開其中具有列編輯模式(如記事本++)編輯該文件並插入空格列在所有你想要的地方。不要用螺絲刀敲釘子。這需要永遠,並破壞螺絲刀。 –